import { z } from "zod";

export const Segment = z.enum(["today", "this_week", "this_month"]);
export type TDateRangeSegment = z.infer<typeof Segment>;

const StatusEnum = z.enum([
  "rejected",
  "inactive",
  "active",
  "visit_pending",
  "compliance_pending",
  "contract_pending",
  "disbursement_pending",
]);

const DateRange = z
  .object({
    segment: Segment.optional(),
    from: z.date().optional().nullable(),
    to: z.date().optional().nullable(),
  })
  .superRefine((val, ctx) => {
    if (val.from && val.to && val.from > val.to) {
      ctx.addIssue({
        code: "custom",
        message: "From date must be before To date",
        path: ["from"],
      });
    }
  });

export const AdvancedDateFilterSchema = z.object({
  dateRange: DateRange.optional(),
  createdAt: DateRange.optional(),
  amount: z
    .number({
      invalid_type_error: "Amount must be a number",
    })
    .min(0, { message: "Amount cannot be negative" })
    // .max(1_000_000, { message: "Amount exceeds maximum limit" })
    .optional()
    .nullable(),
  status: z.array(StatusEnum).optional(),
});

export type AdvancedDateFilterValues = z.infer<typeof AdvancedDateFilterSchema>;
