import { OptionType } from '@/components/input/SelectInput';
import * as yup from 'yup';

export type UniformityTableFilterType = {
  start_date: string;
  end_date: string;
  location: OptionType | null;
  project_flock: OptionType | null;
  project_flock_kandang_id: number | undefined;
  kandang: OptionType | null;
};

export const UniformityTableFilterSchema = yup.object({
  start_date: yup.string().required('Tanggal mulai wajib diisi'),
  end_date: yup
    .string()
    .required('Tanggal akhir wajib diisi')
    .test(
      'is-greater-than-start',
      'Tanggal akhir tidak boleh masa lampau',
      function (value) {
        const { start_date } = this.parent;
        if (!start_date || !value) return true;
        return new Date(value) >= new Date(start_date);
      }
    ),
  location: yup
    .mixed<OptionType>()
    .required('Lokasi wajib dipilih')
    .test('is-not-empty', 'Lokasi wajib dipilih', (value) => {
      if (Array.isArray(value)) {
        return value.length > 0;
      }
      return !!value;
    }),
  project_flock: yup
    .mixed<OptionType>()
    .required('Project Flock wajib dipilih')
    .test('is-not-empty', 'Project Flock wajib dipilih', (value) => {
      if (Array.isArray(value)) {
        return value.length > 0;
      }
      return !!value;
    }),
  project_flock_kandang_id: yup.number().optional(),
  kandang: yup
    .mixed<OptionType>()
    .required('Kandang wajib dipilih')
    .test('is-not-empty', 'Kandang wajib dipilih', (value) => {
      if (Array.isArray(value)) {
        return value.length > 0;
      }
      return !!value;
    }),
}) as yup.ObjectSchema<UniformityTableFilterType>;

export type UniformityTableFilterValues = yup.InferType<
  typeof UniformityTableFilterSchema
>;
