import * as yup from 'yup';

export type DailyMarketingReportFilterType = {
  page?: number;
  pageSize?: number;
  search: string | null;
  area_id: string | null;
  location_id: string | null;
  warehouse_id: string | null;
  customer_id: string | null;
  start_date: string | null;
  end_date: string | null;
  marketing_type: string | null;
  filter_by: string | null;
  sort_by: string | null;
};

export const DailyMarketingReportFilterSchema = yup.object({
  page: yup.number().nullable(),
  pageSize: yup.number().nullable(),
  search: yup.string().nullable(),
  area_id: yup.string().nullable(),
  location_id: yup.string().nullable(),
  warehouse_id: yup.string().nullable(),
  customer_id: yup.string().nullable(),
  start_date: yup.string().nullable(),
  end_date: yup
    .string()
    .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);
      }
    ),
  marketing_type: yup.string().nullable(),
  filter_by: yup.string().nullable(),
  sort_by: yup.string().nullable(),
});

export type DailyMarketingReportFilterValues = yup.InferType<
  typeof DailyMarketingReportFilterSchema
>;
