import { z } from "zod";

const priceStandardSchema = z
  .object({
    start_date: z.string().trim().min(1, "Start date wajib diisi"),
    end_date: z.string().trim().optional().or(z.literal("")),
    min_price: z.coerce.number().min(0, "Min price tidak boleh negatif"),
    max_price: z.coerce.number().min(0, "Max price tidak boleh negatif"),
    notes: z.string().trim().max(2000).optional().or(z.literal(""))
  })
  .refine((value) => value.min_price <= value.max_price, {
    path: ["max_price"],
    message: "Max price harus lebih besar atau sama dengan min price"
  })
  .refine(
    (value) =>
      !value.end_date || !value.start_date || new Date(value.start_date) <= new Date(value.end_date),
    {
      path: ["end_date"],
      message: "End date harus lebih besar atau sama dengan start date"
    }
  );

export const gradeInputSchema = z.object({
  code: z.string().trim().max(20).optional().or(z.literal("")),
  is_mangkok: z.boolean().default(false),
  name: z.string().trim().min(1, "Nama grade wajib diisi").max(150),
  description: z.string().trim().max(2000).optional().or(z.literal("")),
  status: z.enum(["ACTIVE", "INACTIVE"]).default("ACTIVE"),
  buy_price_standards: z.array(priceStandardSchema).default([]),
  sell_price_standards: z.array(priceStandardSchema).default([])
});

export type GradeInput = z.infer<typeof gradeInputSchema>;

