import { z } from 'zod';

const tierRow = z.object({
  minQty: z.coerce.number().int().min(2),
  discountPercent: z.coerce.number().int().min(1).max(100),
});

export const qtyTiersInputSchema = z
  .array(tierRow)
  .max(5)
  .superRefine((rows, ctx) => {
    for (let i = 1; i < rows.length; i++) {
      const prev = rows[i - 1]!;
      const curr = rows[i]!;
      if (curr.minQty <= prev.minQty) {
        ctx.addIssue({ code: 'custom', path: [i, 'minQty'], message: 'minQty must strictly increase' });
      }
      if (curr.discountPercent <= prev.discountPercent) {
        ctx.addIssue({
          code: 'custom',
          path: [i, 'discountPercent'],
          message: 'a higher quantity must give a strictly better discount',
        });
      }
    }
  });

export type QtyTiersInput = z.infer<typeof qtyTiersInputSchema>;
