/**
 * Zod schemas for settlement API routes.
 */

import { z } from 'zod';

/** UUID-shaped id. DB rows (incl. dev fixtures) carry non-RFC version nibbles that strict z.uuid() rejects — validate shape, the lookup decides existence. */
const uuidShaped = z
  .string()
  .regex(/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/);

/** ISO date string (YYYY-MM-DD) */
const dateStringSchema = z
  .string()
  .trim()
  .regex(/^\d{4}-\d{2}-\d{2}$/, 'Date must be in YYYY-MM-DD format');

// ─── Compute monthly settlement ───────────────────────────────────────────────

export const computeSettlementBodySchema = z.object({
  vendorId: uuidShaped,
  periodStart: dateStringSchema,
  periodEnd: dateStringSchema,
});

export type ComputeSettlementBody = z.infer<typeof computeSettlementBodySchema>;

// ─── Run for all vendors ──────────────────────────────────────────────────────

export const runAllSettlementsBodySchema = z.object({
  periodStart: dateStringSchema,
  periodEnd: dateStringSchema,
});

export type RunAllSettlementsBody = z.infer<typeof runAllSettlementsBodySchema>;

// ─── Mark paid ────────────────────────────────────────────────────────────────

const periodMonthSchema = z
  .string()
  .trim()
  .regex(/^\d{4}-(0[1-9]|1[0-2])$/, 'Period must be in YYYY-MM format');

export const markPaidBodySchema = z.object({
  vendorId: uuidShaped,
  periodMonth: periodMonthSchema,
  notes: z.string().trim().max(500).optional(),
});

export type MarkPaidBody = z.infer<typeof markPaidBodySchema>;

// ─── Vendor earnings query ────────────────────────────────────────────────────

export const listVendorEarningsQuerySchema = z.object({
  limit: z.coerce.number().int().min(1).max(100).default(12),
  offset: z.coerce.number().int().min(0).default(0),
});

export type ListVendorEarningsQuery = z.infer<typeof listVendorEarningsQuerySchema>;
