import { z } from "zod";

export const invoiceSchema = z.object({
  customerId: z.string().min(1, "Müşteri seçin."),
  issueDate: z.string().min(1, "Düzenleme tarihi zorunlu."),
  dueDate: z.string().min(1, "Vade tarihi zorunlu."),
  status: z
    .enum(["draft", "sent", "paid", "overdue", "cancelled"])
    .optional()
    .default("draft"),
  notes: z.string().trim().max(2000).optional().transform((v) => (v ? v : undefined)),
});

export type InvoiceInput = z.infer<typeof invoiceSchema>;

export const invoiceItemSchema = z.object({
  description: z.string().trim().min(1, "Açıklama zorunlu.").max(1000),
  quantity: z
    .union([z.number(), z.string()])
    .transform((v) => (typeof v === "string" ? Number(v.replace(",", ".")) : v))
    .pipe(z.number().positive("Miktar 0'dan büyük olmalı.")),
  unitPrice: z
    .union([z.number(), z.string()])
    .transform((v) => (typeof v === "string" ? Number(v.replace(",", ".")) : v))
    .pipe(z.number().nonnegative("Negatif olamaz.")),
  vatRate: z
    .union([z.number(), z.string()])
    .optional()
    .transform((v) => {
      if (v === undefined || v === "") return 0;
      const n = typeof v === "string" ? Number(v.replace(",", ".")) : v;
      return Number.isFinite(n) ? n : 0;
    }),
});

export type InvoiceItemInput = z.infer<typeof invoiceItemSchema>;
