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

export type DashboardFilterType = {
  startDate: string;
  endDate: string;
  analysisMode: string;
  comparisonType: string | undefined;
  location: OptionType | OptionType[];
  locationIds: number[] | undefined;
  flock: OptionType | OptionType[] | undefined;
  flockIds: number[] | undefined;
  kandang: OptionType | OptionType[] | undefined;
  kandangIds: number[] | undefined;
};

// Schema untuk mode OVERVIEW - semua field required
export const DashboardFilterOverviewSchema: yup.ObjectSchema<DashboardFilterType> =
  yup.object({
    startDate: yup.string().required('Start date is required'),
    endDate: yup.string().required('End date is required'),
    analysisMode: yup.string().required('Analysis mode is required'),
    comparisonType: yup.string().when('analysisMode', {
      is: 'COMPARISON',
      then: (schema) => schema.required('Compared by is required'),
      otherwise: (schema) => schema.optional(),
    }),
    locationIds: yup.array().optional(),
    flockIds: yup.array().optional(),
    kandangIds: yup.array().optional(),
    location: yup
      .mixed<OptionType | OptionType[]>()
      .required('Farm is required')
      .test('is-not-empty', 'Farm is required', (value) => {
        if (Array.isArray(value)) {
          return value.length > 0;
        }
        return !!value;
      }),
    flock: yup
      .mixed<OptionType | OptionType[]>()
      .required('Flock is required')
      .test('is-not-empty', 'Flock is required', (value) => {
        if (Array.isArray(value)) {
          return value.length > 0;
        }
        return !!value;
      }),
    kandang: yup
      .mixed<OptionType | OptionType[]>()
      .required('Kandang is required')
      .test('is-not-empty', 'Kandang is required', (value) => {
        if (Array.isArray(value)) {
          return value.length > 0;
        }
        return !!value;
      }),
  });

// Schema untuk mode COMPARISON - conditional validation
export const DashboardFilterComparisonSchema: yup.ObjectSchema<DashboardFilterType> =
  yup.object({
    startDate: yup.string().required('Start date is required'),
    endDate: yup.string().required('End date is required'),
    analysisMode: yup.string().required('Analysis mode is required'),
    comparisonType: yup.string().when('analysisMode', {
      is: 'COMPARISON',
      then: (schema) => schema.required('Compared by is required'),
      otherwise: (schema) => schema.optional(),
    }),
    locationIds: yup.array().optional(),
    flockIds: yup.array().optional(),
    kandangIds: yup.array().optional(),
    location: yup
      .mixed<OptionType | OptionType[]>()
      .required('Farm is required')
      .test('is-not-empty', 'Farm is required', (value) => {
        if (Array.isArray(value)) {
          return value.length > 0;
        }
        return !!value;
      }),
    flock: yup.mixed<OptionType | OptionType[]>().when('comparisonType', {
      is: (value: string) => value === 'FLOCK' || value === 'KANDANG',
      then: (schema) =>
        schema.test('is-required', 'Flock is required', (value) => {
          if (Array.isArray(value)) {
            return value.length > 0;
          }
          return !!value;
        }),
      otherwise: (schema) => schema.optional(),
    }),
    kandang: yup.mixed<OptionType | OptionType[]>().when('comparisonType', {
      is: 'KANDANG',
      then: (schema) =>
        schema.test('is-required', 'Kandang is required', (value) => {
          if (Array.isArray(value)) {
            return value.length > 0;
          }
          return !!value;
        }),
      otherwise: (schema) => schema.optional(),
    }),
  });

// Helper function untuk mendapatkan schema yang sesuai berdasarkan analysis mode
export const getDashboardFilterSchema = (analysisMode?: string) => {
  return analysisMode === 'OVERVIEW'
    ? DashboardFilterOverviewSchema
    : DashboardFilterComparisonSchema;
};

// Default schema
export const DashboardFilterSchema = DashboardFilterComparisonSchema;

export type DashboardFilterValues = yup.InferType<typeof DashboardFilterSchema>;
