import * as Yup from 'yup';

type ProjectFlockFormSchemaType = {
  flock: {
    value: number | string;
    label: string;
  } | null;
  flock_name: string;
  area: {
    value: number | string;
    label: string;
  } | null;
  area_id: number;
  category_option: {
    value: string;
    label: string;
  } | null;
  category: string;
  production_standard: {
    value: number | string;
    label: string;
  } | null;
  production_standard_id: number;
  location: {
    value: number | string;
    label: string;
  } | null;
  location_id: number;
  periode: number | string;
  kandang_ids: number[];
  project_budgets: ProjectFlockBudgetsSchemaType[];
};

export type ProjectFlockBudgetsSchemaType = {
  nonstock: {
    value: number | string;
    label: string;
  } | null;
  nonstock_id: number | string;
  qty: number | string;
  price: number | string;
  total_price: number | string;
};

export const ProjectFlockBudgetsSchema: Yup.ObjectSchema<ProjectFlockBudgetsSchemaType> =
  Yup.object({
    nonstock: Yup.object({
      value: Yup.number().required('ID Nonstock wajib diisi!'),
      label: Yup.string().required('Nama Nonstock wajib diisi!'),
    }).required('Nonstock wajib diisi!'),
    nonstock_id: Yup.number()
      .min(1, 'Nonstock wajib diisi!')
      .required('Nonstock wajib diisi!'),
    qty: Yup.number()
      .typeError('Jumlah harus berupa angka!')
      .min(1, 'Jumlah minimal 1!')
      .required('Jumlah wajib diisi!'),
    price: Yup.number()
      .typeError('Harga harus berupa angka!')
      .min(1, 'Harga minimal 1!')
      .required('Harga wajib diisi!'),
    total_price: Yup.number()
      .typeError('Total Harga harus berupa angka!')
      .min(1, 'Total Harga minimal 1!')
      .required('Total Harga wajib diisi!'),
  });

export const ProjectFlockFormSchema: Yup.ObjectSchema<ProjectFlockFormSchemaType> =
  Yup.object({
    // Flock
    flock: Yup.object({
      value: Yup.number().required('ID Flock wajib diisi!'),
      label: Yup.string().required('Nama Flock wajib diisi!'),
    }).nullable(),
    flock_name: Yup.string().required('Nama Flock wajib diisi!'),

    // Area
    area: Yup.object({
      value: Yup.number().required('ID Area wajib diisi!'),
      label: Yup.string().required('Nama Area wajib diisi!'),
    }).nullable(),
    area_id: Yup.number()
      .min(1, 'Area wajib diisi!')
      .required('Area wajib diisi!'),

    // Kategori
    category_option: Yup.object({
      value: Yup.string().required('Nilai Kategori wajib diisi!'),
      label: Yup.string().required('Label Kategori wajib diisi!'),
    }).nullable(),
    category: Yup.string()
      .oneOf(['GROWING', 'LAYING'], 'Kategori wajib diisi!')
      .required('Kategori wajib diisi!'),

    // Production Standard
    production_standard: Yup.object({
      value: Yup.number().required('ID Standar Produksi wajib diisi!'),
      label: Yup.string().required('Nama Standar Produksi wajib diisi!'),
    }).nullable(),
    production_standard_id: Yup.number()
      .min(1, 'Standar Produksi wajib diisi!')
      .required('Standar Produksi wajib diisi!'),

    // Location
    location: Yup.object({
      value: Yup.number().required('ID Lokasi wajib diisi!'),
      label: Yup.string().required('Nama Lokasi wajib diisi!'),
    }).nullable(),
    location_id: Yup.number()
      .min(1, 'Lokasi wajib diisi!')
      .required('Lokasi wajib diisi!'),

    // Period
    periode: Yup.number()
      .typeError('Periode harus berupa angka!')
      .min(1, 'Periode minimal 1!')
      .required('Periode wajib diisi!'),

    kandang_ids: Yup.array()
      .of(Yup.number().required('Kandang tidak valid!'))
      .min(1, 'Minimal harus ada 1 kandang!')
      .required('Kandang wajib diisi!'),

    project_budgets: Yup.array()
      .of(ProjectFlockBudgetsSchema)
      .min(1, 'Minimal harus ada 1 data budget!')
      .required('Data budget wajib diisi!'),
  });

export type ProjectFlockFormValues = Yup.InferType<
  typeof ProjectFlockFormSchema
>;

export const UpdateProjectFlockFormSchema = ProjectFlockFormSchema;
