import * as yup from 'yup';

export type PurchasesPerSupplierFilterType = {
  start_date: string | null;
  end_date: string | null;
  area_ids: string | null;
  supplier_ids: string | null;
  product_ids: string | null;
  product_category_ids: string | null;
  filter_by: string | null;
  sort_by: string | null;
};

export const PurchasesPerSupplierFilterSchema = yup.object({
  start_date: yup.string().optional().nullable(),
  end_date: yup
    .string()
    .optional()
    .nullable()
    .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);
      }
    ),
  area_ids: yup.string().nullable(),
  supplier_ids: yup.string().nullable(),
  product_ids: yup.string().nullable(),
  product_category_ids: yup.string().nullable(),
  filter_by: yup.string().nullable(),
  sort_by: yup.string().nullable(),
});

export type PurchasesPerSupplierFilterValues = yup.InferType<
  typeof PurchasesPerSupplierFilterSchema
>;
