import * as Yup from 'yup';
import { SalesOrderProductFormValues } from '@/components/pages/marketing/form/repeater/sales-order/SalesOrderProduct.schema';

type DeliveryOrderProductSchemaType = {
  id?: number | undefined;
  marketing_product_id: number | undefined; // Sales Order ID
  marketing_product?: SalesOrderProductFormValues | undefined | null;
  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;
  delivery_date: string | undefined | null;
  do_number?: string | undefined | null; // Uncertain
  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 DeliveryOrderProductSchema: Yup.ObjectSchema<DeliveryOrderProductSchemaType> =
  Yup.object({
    id: Yup.number(),
    marketing_product_id: Yup.number()
      .min(1, 'Produk wajib diisi!')
      .required('Produk wajib diisi!'),
    marketing_product: Yup.object().nullable().optional(),
    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!'),
    vehicle_number: Yup.string().required('Nomor Kendaraan wajib diisi!'),
    delivery_date: Yup.string().required('Tanggal Pengiriman wajib diisi!'),
    do_number: Yup.string().nullable().optional(),
    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 DeliveryOrderProductFormValues = Yup.InferType<
  typeof DeliveryOrderProductSchema
>;
