import * as Yup from 'yup';

// Schema for LAYING category (production_standard_details is required)
const LayingRepeaterFormSchema = Yup.object({
  week: Yup.number().required('Wajib diisi!'),
  production_standard_uniformity_details: Yup.object({
    target_mean_bw: Yup.number().required('Wajib diisi!'),
    max_depletion: Yup.number().required('Wajib diisi!'),
    min_uniformity: Yup.number().required('Wajib diisi!'),
    feed_intake: Yup.number().required('Wajib diisi!'),
  }),
  production_standard_details: Yup.object({
    target_hen_day_production: Yup.number().required('Wajib diisi!'),
    target_hen_house_production: Yup.number().required('Wajib diisi!'),
    target_egg_weight: Yup.number().required('Wajib diisi!'),
    target_egg_mass: Yup.number().required('Wajib diisi!'),
    standard_fcr: Yup.number().required('Wajib diisi!'),
  }).required(),
});

// Schema for GROWING category (production_standard_details is optional)
const GrowingRepeaterFormSchema = Yup.object({
  week: Yup.number().required('Wajib diisi!'),
  production_standard_uniformity_details: Yup.object({
    target_mean_bw: Yup.number().required('Wajib diisi!'),
    max_depletion: Yup.number().required('Wajib diisi!'),
    min_uniformity: Yup.number().required('Wajib diisi!'),
    feed_intake: Yup.number().required('Wajib diisi!'),
  }),
  production_standard_details: Yup.object({
    target_hen_day_production: Yup.number().optional(),
    target_hen_house_production: Yup.number().optional(),
    target_egg_weight: Yup.number().optional(),
    target_egg_mass: Yup.number().optional(),
    standard_fcr: Yup.number().required('Wajib diisi!'),
  }).optional(),
});

// Explicit types for better type inference
export type LayingRepeaterFormValues = Yup.InferType<
  typeof LayingRepeaterFormSchema
>;
export type GrowingRepeaterFormValues = Yup.InferType<
  typeof GrowingRepeaterFormSchema
>;

// Union type for repeater form values
export type ProductionStandardRepeaterFormSchemaValues =
  | LayingRepeaterFormValues
  | GrowingRepeaterFormValues;

// Dynamic schema factory for repeater form based on project category
export const createProductionStandardRepeaterFormSchema = (
  category: string
) => {
  // For LAYING category, production_standard_details is required
  if (category === 'LAYING') {
    return LayingRepeaterFormSchema;
  }

  // For GROWING category, production_standard_details is optional
  return GrowingRepeaterFormSchema;
};

// Dynamic schema factory for main form based on project category
export const createProductionStandardFormSchema = (category: string) => {
  return Yup.object({
    name: Yup.string().required('Nama wajib diisi!'),
    project_category: Yup.string()
      .min(1, 'Kategori proyek wajib diisi!')
      .required('Kategori proyek wajib diisi!'),
    details: Yup.array().of(
      createProductionStandardRepeaterFormSchema(category)
    ),
  });
};

// Static schemas for backward compatibility (default to LAYING)
export const ProductionStandardFormSchema =
  createProductionStandardFormSchema('LAYING');

export const UpdateProductionStandardFormSchema = ProductionStandardFormSchema;

export type ProductionStandardFormValues = Yup.InferType<
  typeof ProductionStandardFormSchema
>;

export const ProductionStandardRepeaterFormSchema = LayingRepeaterFormSchema;

export const UpdateProductionStandardRepeaterFormSchema =
  ProductionStandardRepeaterFormSchema;
