import { z } from "zod";
import { INVOICE_STATUSES } from "../constants/index.js";

export const invoiceStatusSchema = z.enum(INVOICE_STATUSES);

export const createInvoiceSchema = z.object({
  clientId: z.string().uuid("Client invalide"),
  numero: z
    .string()
    .min(1, "Numéro requis")
    .max(50, "50 caractères maximum"),
  amountTtcCents: z
    .number({ invalid_type_error: "Montant invalide" })
    .int("Montant en centimes (entier)")
    .positive("Le montant doit être positif"),
  issueDate: z.string().datetime({ message: "Date d'émission invalide" }),
  dueDate: z.string().datetime({ message: "Date d'échéance invalide" }),
  planId: z.string().uuid().nullable().optional(),
  notes: z.string().max(2000).nullable().optional(),
});

export const updateInvoiceSchema = createInvoiceSchema.partial();

/** Schéma des filtres URL — utilisé par TanStack Router validateSearch. */
export const invoiceListFiltersSchema = z.object({
  status: invoiceStatusSchema.or(z.literal("all")).optional(),
  q: z.string().optional(),
  clientId: z.string().uuid().optional(),
  page: z.number().int().min(1).optional().default(1),
});

export type CreateInvoiceInput = z.infer<typeof createInvoiceSchema>;
export type UpdateInvoiceInput = z.infer<typeof updateInvoiceSchema>;
export type InvoiceListFiltersInput = z.infer<typeof invoiceListFiltersSchema>;
