import { z } from "zod"
import {
    JobCardStatus,
    EstimateTo,
    TaxInclusive,
    DiscountType,
    DiscountAt,
    FuelLevel,
} from "@garage/api"

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

// ── Job Card Statuses ──

export const JOB_CARD_STATUS_OPTIONS = JobCardStatus.map((v) => ({
    value: v,
    label: v
        .replace(/_/g, " ")
        .replace(/\b\w/g, (c) => c.toUpperCase()),
}))

const ESTIMATE_TO_OPTIONS = EstimateTo.map((v) => ({ value: v, label: v }))

const TAX_INCLUSIVE_OPTIONS = TaxInclusive.map((v) => ({ value: v, label: v }))

const DISCOUNT_TYPE_OPTIONS = DiscountType.map((v) => ({
    value: v,
    label: v
        .replace(/_/g, " ")
        .replace(/\b\w/g, (c) => c.toUpperCase()),
}))

const DISCOUNT_AT_OPTIONS = DiscountAt.map((v) => ({
    value: v,
    label: v
        .replace(/_/g, " ")
        .replace(/\b\w/g, (c) => c.toUpperCase()),
}))

const FUEL_LEVEL_OPTIONS = FuelLevel.map((v) => ({
    value: v,
    label: v
        .replace(/_/g, " ")
        .replace(/\b\w/g, (c) => c.toUpperCase()),
}))

const jobCardFormSchema = z.object({
    // ── Required fields ──
    title: z.string().min(1, "Title is required"),

    // ── Relations ──
    customer: relationFieldSchema,
    vehicle: relationFieldSchema,
    department: relationFieldSchema,
    service_writer: relationFieldSchema,
    primary_technician: relationFieldSchema,
    sales_person: relationFieldSchema,
    insurance_type: relationFieldSchema,
    insurer: relationFieldSchema,
    tax: relationFieldSchema,

    // ── Numbers & identifiers ──
    order_number: z.string().optional(),
    estimate_number: z.string().optional(),

    // ── Status & settings ──
    status: z.string().optional(),
    estimate_to: z.string().optional(),
    tax_inclusive: z.string().optional(),
    discount_type: z.string().optional(),
    discount_at: z.string().optional(),

    // ── Dates & times ──
    order_date: z.string().optional(),
    check_in_date: z.string().optional(),
    check_in_time: z.string().optional(),
    start_date: z.string().optional(),
    start_time: z.string().optional(),
    delivery_date: z.string().optional(),
    delivery_time: z.string().optional(),

    // ── Vehicle state ──
    km_in: z.string().optional(),
    fuel_level: z.string().optional(),

    // ── Boolean options ──
    has_insurance: z.boolean().optional(),
    enable_parts_issuing: z.boolean().optional(),
    enable_digital_authorisation: z.boolean().optional(),

    // ── Notes ──
    footer: z.string().optional(),

    // ── Customer Remarks ──
    customer_remarks: z.array(z.string()).optional(),

    // ── Labels ──
    labels: z
        .array(
            z.object({
                id: z.number(),
                title: z.string(),
                color_code: z.string(),
            }),
        )
        .optional(),
})

type JobCardFormValues = z.infer<typeof jobCardFormSchema>

export {
    jobCardFormSchema,
    relationFieldSchema,
    TAX_INCLUSIVE_OPTIONS,
    DISCOUNT_TYPE_OPTIONS,
    DISCOUNT_AT_OPTIONS,
    ESTIMATE_TO_OPTIONS,
    FUEL_LEVEL_OPTIONS,
}
// Backward-compat alias used by job-card-status-stepper
export const JOB_CARD_STATUSES = JOB_CARD_STATUS_OPTIONS
export type { JobCardFormValues }
export type { JobCardStatus } from "@garage/api"
