import * as Yup from 'yup';
import { Purchase } from '@/types/api/purchase/purchase';

type PurchaseRequestFormSchemaType = {
  supplier?: {
    value: number;
    label: string;
  } | null;
  supplier_id: number;
  credit_term: number;
  area?: {
    value: number;
    label: string;
  } | null;
  area_id: number | undefined;
  location?: {
    value: number;
    label: string;
  } | null;
  location_id: number | undefined;
  notes: string | null;
  items: {
    warehouse?: {
      value: number;
      label: string;
    } | null;
    warehouse_id: number;
    product?: {
      value: number;
      label: string;
    } | null;
    product_id: number;
    qty: number;
  }[];
};

export type PurchaseItemSchema = {
  warehouse?: {
    value: number;
    label: string;
  } | null;
  warehouse_id: number;
  product?: {
    value: number;
    label: string;
  } | null;
  product_id: number;
  qty: number;
};

const PurchaseItemObjectSchema: Yup.ObjectSchema<PurchaseItemSchema> =
  Yup.object({
    warehouse: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    }).nullable(),
    warehouse_id: Yup.number()
      .required('Gudang wajib dipilih!')
      .min(1, 'Gudang wajib dipilih!')
      .typeError('Gudang wajib dipilih!'),
    product: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    }).nullable(),
    product_id: Yup.number()
      .required('Produk wajib dipilih!')
      .min(1, 'Produk wajib dipilih!')
      .typeError('Produk wajib dipilih!'),
    qty: Yup.number()
      .required('Kuantitas wajib diisi!')
      .min(1, 'Kuantitas tidak boleh kurang dari 1!')
      .typeError('Kuantitas wajib diisi!'),
  });

export const PurchaseRequestFormSchema: Yup.ObjectSchema<PurchaseRequestFormSchemaType> =
  Yup.object({
    supplier: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    }).nullable(),
    supplier_id: Yup.number()
      .required('Supplier wajib dipilih!')
      .min(1, 'Supplier wajib dipilih!')
      .typeError('Supplier wajib dipilih!'),
    credit_term: Yup.number()
      .required('Jangka waktu kredit wajib diisi!')
      .min(0, 'Jangka waktu kredit tidak boleh kurang dari 0!')
      .typeError('Jangka waktu kredit wajib diisi!'),
    area: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    }).nullable(),
    area_id: Yup.number()
      .min(0, 'Area tidak boleh kurang dari 0!')
      .typeError('Area harus berupa angka!'),
    location: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    }).nullable(),
    location_id: Yup.number()
      .min(0, 'Lokasi tidak boleh kurang dari 0!')
      .typeError('Lokasi harus berupa angka!'),
    notes: Yup.string().nullable().default(null),
    items: Yup.array()
      .of(PurchaseItemObjectSchema)
      .min(1, 'Minimal harus ada 1 item pembelian!')
      .required('Item pembelian wajib diisi!')
      .typeError('Item pembelian wajib diisi!'),
  });

export const UpdatePurchaseRequestFormSchema = PurchaseRequestFormSchema;

export type PurchaseRequestFormValues = Yup.InferType<
  typeof PurchaseRequestFormSchema
>;

export const getPurchaseRequestFormInitialValues = (
  initialValues?: Purchase
): PurchaseRequestFormValues => ({
  supplier: initialValues?.supplier
    ? {
        value: initialValues.supplier.id,
        label: initialValues.supplier.name,
      }
    : null,
  supplier_id: initialValues?.supplier?.id ?? 0,
  credit_term: initialValues?.credit_term ?? 0,
  area: initialValues?.area
    ? {
        value: initialValues.area.id,
        label: initialValues.area.name,
      }
    : null,
  area_id: initialValues?.area?.id ?? undefined,
  location: initialValues?.location
    ? {
        value: initialValues.location.id,
        label: initialValues.location.name,
      }
    : null,
  location_id: initialValues?.location?.id ?? undefined,
  notes: initialValues?.notes ?? null,
  items: initialValues?.items?.length
    ? initialValues.items.map((item) => ({
        warehouse: item.warehouse
          ? {
              value: item.warehouse.id,
              label: item.warehouse.name,
            }
          : null,
        warehouse_id: item.warehouse?.id ?? 0,
        product: item.product
          ? {
              value: item.product.id,
              label: item.product.name,
            }
          : null,
        product_id: item.product?.id ?? 0,
        qty: item.qty ?? 0,
      }))
    : [
        {
          warehouse: null,
          warehouse_id: 0,
          product: null,
          product_id: 0,
          qty: 0,
        },
      ],
});
