import { z } from 'zod';

// ── Request ID Params (shared across all /:requestId routes) ──

export const requestIdParamsSchema = z.object({
    requestId: z.string().min(1, 'Request ID is required').refine(
        (val) => {
            // Accept UUID format
            const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
            // Accept request number format (e.g., REQ-2026-02-0048)
            const requestNumberRegex = /^REQ-\d{4}-\d{2}-\d{4,}$/i;
            return uuidRegex.test(val) || requestNumberRegex.test(val);
        },
        { message: 'Invalid request ID — must be a UUID or request number (REQ-YYYY-MM-NNNN)' }
    ),
});

// ── Create Claim Schema ──

export const createClaimSchema = z.object({
    dealerId: z.string().optional(),
    activityTypeId: z.string().uuid('Invalid activity type ID').optional(),
    title: z.string().min(1, 'Title is required').max(500, 'Title too long').optional(),
    description: z.string().max(5000, 'Description too long').optional(),
}).passthrough(); // Allow additional fields since dealer claims have flexible structure

// ── IO Details Schema ──

export const updateIOSchema = z.object({
    ioNumber: z.string().min(1, 'IO number is required').max(50, 'IO number too long'),
    amount: z.number().min(0, 'Amount cannot be negative').optional(),
    requestNumber: z.string().max(50).optional(),
}).passthrough();

// ── E-Invoice Schema ──

export const updateEInvoiceSchema = z.object({
    irnNumber: z.string().max(100, 'IRN number too long').optional(),
    ackNumber: z.string().max(100, 'Acknowledgement number too long').optional(),
    invoiceNumber: z.string().max(100, 'Invoice number too long').optional(),
    invoiceDate: z.string().optional(),
    amount: z.number().min(0, 'Amount cannot be negative').optional(),
}).passthrough();

// ── Credit Note Schema ──

export const sendCreditNoteSchema = z.object({
    creditNoteNumber: z.string().max(100, 'Credit note number too long').optional(),
    creditNoteDate: z.string().optional(),
    creditNoteAmount: z.number().min(0, 'Amount cannot be negative').optional(),
}).passthrough();

// ── SAP Test Schema ──

export const testSapBlockSchema = z.object({
    ioNumber: z.string().min(1, 'IO number is required'),
    amount: z.number().min(0, 'Amount cannot be negative'),
    requestNumber: z.string().optional(),
});

// ── Proposal & Completion Schemas ──

export const costItemSchema = z.object({
    description: z.string().min(1, 'Description is required'),
    amount: z.number().gt(0, 'Amount must be greater than zero'),
    quantity: z.number().min(1, 'Quantity must be at least 1').optional(),
    totalAmt: z.number().gt(0, 'Total amount must be greater than zero').optional(),
}).passthrough();

export const costBreakupSchema = z.array(costItemSchema);

export const expenseItemSchema = z.object({
    description: z.string().min(1, 'Description is required'),
    amount: z.number().gt(0, 'Amount must be greater than zero'),
    quantity: z.number().min(1, 'Quantity must be at least 1').optional(),
    totalAmt: z.number().gt(0, 'Total amount must be greater than zero').optional(),
}).passthrough();

export const closedExpensesSchema = z.array(expenseItemSchema);
