import * as z from "zod";

export const paymentPlanSchema = z
  .object({
    project: z.string().min(1, {
      message: "Please select a project",
    }),
    name: z.string().min(1, { message: "Name is required" }),
    ratePerSqYard: z.coerce
      .number()
      .min(1, { message: "Rate must be greater than 0" }),
    downPaymentType: z.enum(["percentage", "fixedAmount"], {
      required_error: "Please select down payment type",
    }),
    downPaymentValue: z.coerce.number({
      required_error: "Please enter a down payment value",
    }),
    installmentMonths: z.coerce
      .number()
      .min(1, { message: "Installment months must be at least 1" }),
    status: z.enum(["active", "inactive"], {
      required_error: "Please select status",
    }),
  })
  .refine(
    (data) =>
      data.downPaymentType === "percentage"
        ? data.downPaymentValue >= 0 && data.downPaymentValue <= 100
        : data.downPaymentValue > 0,
    {
      path: ["downPaymentValue"],
      message:
        "For percentage, value must be between 0–100; for fixed amount, it must be greater than 0",
    }
  );
