import { z } from "zod";

const dateRegex = /^\d{4}-\d{2}-\d{2}$/;

const nullableString = z.union([z.string().trim().min(1).max(255), z.null()]).optional();
const nullableDate = z.union([z.string().regex(dateRegex, "Expected YYYY-MM-DD format"), z.null()]).optional();
const nullableInteger = z.union([z.number().int().nonnegative(), z.null()]).optional();
const nullableNumber = z.union([z.number().nonnegative(), z.null()]).optional();

export const contractCreateSchema = z.object({
  title: z.string().min(1).max(255),
  paperlessDocumentId: nullableInteger,
  provider: nullableString,
  category: nullableString,
  contractStartDate: nullableDate,
  contractEndDate: nullableDate,
  terminationNoticeDays: nullableInteger,
  renewalPeriodMonths: nullableInteger,
  autoRenew: z.boolean().optional().default(false),
  price: nullableNumber,
  currency: z.string().min(1).max(8).optional().default("EUR"),
  notes: z.union([z.string().max(5000), z.null()]).optional(),
  tags: z.array(z.string().min(1).max(100)).optional().default([])
});

export const contractUpdateSchema = contractCreateSchema.partial();
