import { z } from "zod";
import { numberInRange, nonNegativeNumberFromForm } from "./common";

export const CreateReceivedInvoiceSchema = z.object({
  supplier_name: z.string().min(1, "Název dodavatele je povinný").max(255),
  month: numberInRange(1, 12),
  year: numberInRange(2000, 2100),
  invoice_number: z.string().max(255).nullish(),
  description: z.string().max(8000).nullish(),
  amount: nonNegativeNumberFromForm.optional().default(0),
  currency: z.string().max(20).optional().default("CZK"),
  vat_rate: numberInRange(0, 100).optional().default(21),
  vat_amount: nonNegativeNumberFromForm.optional().default(0),
  issue_date: z.string().max(255).nullish(),
  due_date: z.string().max(255).nullish(),
  status: z.enum(["unpaid", "paid"]).optional().default("unpaid"),
  notes: z.string().max(8000).nullish(),
});

export const UpdateReceivedInvoiceSchema = z.object({
  supplier_name: z.string().max(255).optional(),
  invoice_number: z.string().max(255).nullish(),
  description: z.string().max(8000).nullish(),
  amount: nonNegativeNumberFromForm.optional(),
  currency: z.string().max(20).optional(),
  vat_rate: numberInRange(0, 100).optional(),
  vat_amount: nonNegativeNumberFromForm.optional(),
  issue_date: z.union([z.string().max(255), z.null()]).optional(),
  due_date: z.union([z.string().max(255), z.null()]).optional(),
  paid_date: z.union([z.string().max(255), z.null()]).optional(),
  status: z.enum(["unpaid", "paid"]).optional(),
  notes: z.string().max(8000).nullish(),
  month: numberInRange(1, 12).optional(),
  year: numberInRange(2000, 2100).optional(),
});

export type CreateReceivedInvoiceInput = z.infer<
  typeof CreateReceivedInvoiceSchema
>;
export type UpdateReceivedInvoiceInput = z.infer<
  typeof UpdateReceivedInvoiceSchema
>;
