import { ProductWarehouse } from '@/types/api/inventory/product-warehouse';
import * as Yup from 'yup';

type SalesOrderProductSchemaType = {
  id?: number | undefined;
  warehouse_id?: number;
  warehouse?: {
    value: number;
    label: string;
  } | null;
  kandang_id?: number;
  kandang?: {
    value: number;
    label: string;
  } | null;
  product_warehouse?: {
    value: number;
    label: string;
  } | null;
  product_warehouse_data?: ProductWarehouse | null | undefined;
  product_warehouse_id?: number;
  unit_price: string | number | undefined;
  total_weight: string | number | undefined;
  qty: string | number | undefined;
  avg_weight: string | number | undefined;
  total_price: string | number | undefined;
  vehicle_number?: string | undefined;
  uom?: string | null | undefined;
  convertion_unit?: {
    value: string;
    label: string;
  } | null;
  weight_per_convertion?: number | null | undefined;
  price_per_convertion?: number | null | undefined;
  marketing_type?: {
    value: string;
    label: string;
  } | null;
  total_peti?: number | null | undefined;
  sisa_berat?: number | null | undefined;
  price_sisa_berat?: number | null | undefined;
  /** Harga per butir telur untuk TELUR + QTY */
  price_per_qty?: number | null | undefined;
  /** Week untuk ayam pullet */
  week?: number | null | undefined;
};

export const SalesOrderProductSchema: Yup.ObjectSchema<SalesOrderProductSchemaType> =
  Yup.object({
    id: Yup.number(),
    vehicle_number: Yup.string().required('Nomor Kendaraan wajib diisi!'),
    warehouse: Yup.object({
      value: Yup.number()
        .min(1, 'Gudang fisik wajib diisi!')
        .required('Gudang fisik wajib diisi!'),
      label: Yup.string().required('Gudang fisik wajib diisi!'),
    }).nullable(),
    warehouse_id: Yup.number()
      .min(1, 'Gudang fisik wajib diisi!')
      .required('Gudang fisik wajib diisi!'),
    kandang: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    })
      .nullable()
      .optional(),
    kandang_id: Yup.number().optional(),
    product_warehouse: Yup.object({
      value: Yup.number()
        .min(1, 'Produk wajib diisi!')
        .required('Produk wajib diisi!'),
      label: Yup.string().required('Produk wajib diisi!'),
    }).nullable(),
    product_warehouse_data: Yup.mixed<ProductWarehouse>()
      .nullable()
      .optional()
      .notRequired(),
    product_warehouse_id: Yup.number()
      .min(1, 'Produk wajib diisi!')
      .required('Produk wajib diisi!'),
    unit_price: Yup.number()
      .min(1, 'Harga Satuan wajib diisi!')
      .required('Harga Satuan wajib diisi!'),
    total_weight: Yup.number()
      .min(0, 'Total Bobot wajib diisi!')
      .required('Total Bobot wajib diisi!'),
    qty: Yup.number()
      .min(1, 'Kuantitas wajib diisi!')
      .required('Kuantitas wajib diisi!'),
    avg_weight: Yup.number()
      .min(0, 'Avg. Bobot wajib diisi!')
      .required('Avg. Bobot wajib diisi!'),
    total_price: Yup.number()
      .min(1, 'Total Penjualan wajib diisi!')
      .required('Total Penjualan wajib diisi!'),
    uom: Yup.string().nullable().optional().notRequired(),
    convertion_unit: Yup.object({
      value: Yup.string().required('Konversi Satuan wajib diisi!'),
      label: Yup.string().required('Konversi Satuan wajib diisi!'),
    }).nullable(),
    weight_per_convertion: Yup.number().nullable().optional().notRequired(),
    marketing_type: Yup.object({
      value: Yup.string().required('Kategori Penjualan wajib diisi!'),
      label: Yup.string().required('Kategori Penjualan wajib diisi!'),
    }).nullable(),
    price_per_convertion: Yup.number().nullable().optional().notRequired(),
    total_peti: Yup.number().nullable().optional().notRequired(),
    sisa_berat: Yup.number().nullable().optional().notRequired(),
    price_sisa_berat: Yup.number().nullable().optional().notRequired(),
    price_per_qty: Yup.number().nullable().optional().notRequired(),
    week: Yup.number()
      .nullable()
      .optional()
      .notRequired()
      .when('marketing_type', {
        is: (marketingType: { value: string } | null | undefined) =>
          marketingType?.value?.toLowerCase() === 'ayam_pullet',
        then: (schema) =>
          schema
            .min(1, 'Week wajib diisi untuk Ayam Pullet!')
            .required('Week wajib diisi untuk Ayam Pullet!')
            .typeError('Week harus berupa angka!'),
        otherwise: (schema) => schema.optional().notRequired(),
      }),
  });

export type SalesOrderProductFormValues = Yup.InferType<
  typeof SalesOrderProductSchema
>;
