import * as Yup from 'yup';

export type InventoryAdjustmentFormSchemaType = {
  location: {
    value: number;
    label: string;
  } | null;
  location_id: number;
  project_flock: {
    value: number;
    label: string;
  } | null;
  project_flock_id: number;
  kandang: {
    value: number;
    label: string;
  } | null;
  kandang_id: number;
  project_flock_kandang: {
    value: number;
    label: string;
  } | null;
  project_flock_kandang_id: number;
  product: {
    value: number;
    label: string;
  } | null;
  product_id: number;
  depletion_product: {
    value: number;
    label: string;
  } | null;
  depletion_product_id: number;
  transaction_type: string;
  transaction_subtype: string;
  qty: number | string;
  price: number | string;
  notes: string;
};

export const InventoryAdjustmentFormSchema: Yup.ObjectSchema<InventoryAdjustmentFormSchemaType> =
  Yup.object({
    location: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    }).nullable(),
    location_id: Yup.number()
      .min(1, 'Lokasi wajib diisi!')
      .required('Lokasi wajib diisi!')
      .typeError('Lokasi wajib diisi!'),
    project_flock: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    }).nullable(),
    project_flock_id: Yup.number()
      .min(1, 'Project flock wajib diisi!')
      .required('Project flock wajib diisi!')
      .typeError('Project flock wajib diisi!'),
    kandang: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    }).nullable(),
    kandang_id: Yup.number()
      .min(1, 'Kandang wajib diisi!')
      .required('Kandang wajib diisi!')
      .typeError('Kandang wajib diisi!'),
    project_flock_kandang: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    }).nullable(),
    project_flock_kandang_id: Yup.number()
      .default(0)
      .typeError('Project Flock Kandang wajib diisi!')
      .test(
        'is-valid-project-flock-kandang',
        'Project Flock Kandang wajib diisi!',
        (value) => value !== undefined && value !== null && value > 0
      )
      .required('Project Flock Kandang wajib diisi!'),
    product: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    }).nullable(),
    product_id: Yup.number()
      .min(1, 'Produk wajib diisi!')
      .required('Produk wajib diisi!')
      .typeError('Produk wajib diisi!'),
    depletion_product: Yup.object({
      value: Yup.number().min(1).required(),
      label: Yup.string().required(),
    }).nullable(),
    depletion_product_id: Yup.number()
      .default(0)
      .typeError('Jenis deplesi harus berupa angka'),
    transaction_type: Yup.string()
      .min(1, 'Tipe transaksi wajib diisi!')
      .oneOf(
        ['PEMBELIAN', 'PENJUALAN', 'RECORDING'],
        'Tipe transaksi tidak valid'
      )
      .required('Tipe transaksi wajib diisi')
      .typeError('Tipe transaksi wajib diisi!'),
    transaction_subtype: Yup.string().required(
      'Sub tipe transaksi wajib diisi'
    ),
    qty: Yup.number()
      .typeError('Kuantitas harus berupa angka')
      .min(1, 'Minimal kuantitas adalah 1')
      .required('Kuantitas wajib diisi'),
    price: Yup.number()
      .typeError('Harga harus berupa angka')
      .min(0, 'Minimal harga adalah 0')
      .required('Harga wajib diisi'),
    notes: Yup.string().required('Catatan wajib diisi!'),
  });

export type InventoryAdjustmentFormValues = Yup.InferType<
  typeof InventoryAdjustmentFormSchema
>;
