import * as yup from 'yup';

export type HppPerKandangFilterType = {
  page?: number;
  pageSize?: number;
  area_id: string | null;
  location_id: string | null;
  kandang_id: string | null;
  weight_min: string | null;
  weight_max: string | null;
  period: string | null;
  sort_by: string | null;
  show_unrecorded: boolean | null;
};

export const HppPerKandangFilterSchema = yup.object({
  page: yup.number().nullable(),
  pageSize: yup.number().nullable(),
  area_id: yup.string().nullable(),
  location_id: yup.string().nullable(),
  kandang_id: yup.string().nullable(),
  weight_min: yup.string().nullable(),
  weight_max: yup
    .string()
    .nullable()
    .test(
      'is-greater-than-min',
      'Rentang bobot max tidak boleh lebih kecil dari min',
      function (value) {
        const { weight_min } = this.parent;
        if (!weight_min || !value) return true;
        const weightMinNum = parseFloat(weight_min) || 0;
        const weightMaxNum = parseFloat(value) || 0;
        return weightMaxNum >= weightMinNum;
      }
    ),
  period: yup.string().required('Periode wajib diisi'),
  sort_by: yup.string().nullable(),
  show_unrecorded: yup.boolean().nullable(),
});

export type HppPerKandangFilterValues = yup.InferType<
  typeof HppPerKandangFilterSchema
>;
