import { z } from "zod"

const relationFieldSchema = z
    .object({ value: z.string(), label: z.string() })
    .nullable()

const purchaseOrderItemSchema = z.object({
    part_id: z.number(),
    title: z.string(),
    quantity: z.number().min(1, "Quantity must be at least 1"),
    rate: z.number().min(0, "Rate must be non-negative"),
    description: z.string().optional(),
})

const purchaseOrderFormSchema = z.object({
    // ── Relations ──
    vendor: relationFieldSchema,
    job_card: relationFieldSchema,
    department: relationFieldSchema,

    // ── Basic info ──
    title: z.string().min(1, "Title is required"),
    order_date: z.string().optional(),
    delivery_date: z.string().optional(),
    notes: z.string().optional(),
    order_number: z.string().min(1, "Order number is required"),

    // ── Items (parts) ──
    items: z.array(purchaseOrderItemSchema).optional(),
})

type PurchaseOrderFormValues = z.infer<typeof purchaseOrderFormSchema>
type PurchaseOrderItem = z.infer<typeof purchaseOrderItemSchema>

export { purchaseOrderFormSchema, purchaseOrderItemSchema, relationFieldSchema }
export type { PurchaseOrderFormValues, PurchaseOrderItem }
 
