"use client"

import { z } from "zod"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { toast } from "sonner"
import { useAuthApi } from "@/shared/useApi"
import {
    Rhform,
    RhfTextField,
    RhfSelectField,
    RhfDateField,
    RhfTimeField,
    RhfAsyncSelectField,
} from "@/shared/components/form"
import { FieldGroup } from "@/shared/components/ui/field"
import { Button } from "@/shared/components/ui/button"
import {
    Dialog,
    DialogContent,
    DialogHeader,
    DialogTitle,
} from "@/shared/components/ui/dialog"
import { DEPARTMENT_ROUTES } from "@garage/api"
import { FUEL_LEVEL_OPTIONS } from "./job-card.schema"
import { toId } from "@/shared/lib/utils"
import { DepartmentInlineForm } from "@/modules/services/inline-forms/department-inline-form"

// ── Schema ──

const checkInSchema = z.object({
    check_in_date: z.string().optional(),
    check_in_time: z.string().optional(),
    km_in: z.string().optional(),
    fuel_level: z.string().optional(),
    start_date: z.string().optional(),
    start_time: z.string().optional(),
    department: z.object({ value: z.any(), label: z.string() }).nullable().optional(),
    delivery_date: z.string().optional(),
    delivery_time: z.string().optional(),
}).superRefine((values, ctx) => {
    // Department is marked required in the UI (asterisk); enforce it at the
    // schema level so the form cannot be submitted without one.
    if (!values.department) {
        ctx.addIssue({
            code: z.ZodIssueCode.custom,
            path: ["department"],
            message: "Department is required",
        })
    }
})

type CheckInFormValues = z.infer<typeof checkInSchema>

const mapLookupOption = (item: any) => ({
    value: String(item.id),
    label: item.name,
})

const STORE_OBJECT = { getOptionValue: (o: any) => o, getOptionLabel: (o: any) => o.label } as const

// ── Props ──

type JobCardCheckInDialogProps = {
    jobCardId: string
    open: boolean
    onOpenChange: (open: boolean) => void
    onSuccess?: () => void
}

// ── Component ──

export function JobCardCheckInDialog({
    jobCardId,
    open,
    onOpenChange,
    onSuccess,
}: JobCardCheckInDialogProps) {
    const api = useAuthApi()

    const now = new Date()
    const todayStr = now.toISOString().split("T")[0]
    const currentTime = `${String(now.getHours()).padStart(2, "0")}:${String(now.getMinutes()).padStart(2, "0")}:${String(now.getSeconds()).padStart(2, "0")}`

    const form = useForm<CheckInFormValues>({
        resolver: zodResolver(checkInSchema),
        defaultValues: {
            check_in_date: todayStr,
            check_in_time: currentTime,
            km_in: "",
            fuel_level: "",
            start_date: "",
            start_time: "",
            department: null,
            delivery_date: "",
            delivery_time: "",
        },
    })

    const handleSubmit = async (values: CheckInFormValues) => {
        if (!values.department) {
            form.setError("department", { message: "Department is required" })
            return
        }
        try {
            await api.jobCards.checkIn(jobCardId, {
                check_in_date: values.check_in_date || undefined,
                check_in_time: values.check_in_time || undefined,
                km_in: values.km_in ? Number(values.km_in) : undefined,
                fuel_level: values.fuel_level || undefined,
                start_date: values.start_date || undefined,
                start_time: values.start_time || undefined,
                department_id: values.department ? Number(toId(values.department)) : undefined,
                delivery_date: values.delivery_date || undefined,
                delivery_time: values.delivery_time || undefined,
            })
            toast.success("Job card checked in successfully")
            form.reset()
            onSuccess?.()
        } catch {
            toast.error("Failed to check in job card")
        }
    }

    return (
        <Dialog open={open} onOpenChange={onOpenChange}>
            <DialogContent className="min-w-xl">
                <DialogHeader>
                    <DialogTitle className="text-2xl font-bold">Check In</DialogTitle>
                </DialogHeader>
                <Rhform form={form} onSubmit={handleSubmit}>
                    <FieldGroup>
                        <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                            <RhfDateField name="check_in_date" label="Check-in Date" />
                            <RhfTimeField name="check_in_time" label="Check-in Time" />
                        </div>
                        <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                            <RhfTextField name="km_in" label="KM In" type="number" placeholder="e.g. 50321" />
                            <RhfSelectField name="fuel_level" label="Fuel Level" options={FUEL_LEVEL_OPTIONS} />
                        </div>
                        <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                            <RhfDateField name="start_date" label="Start Date" />
                            <RhfTimeField name="start_time" label="Start Time" />
                        </div>
                        <RhfAsyncSelectField
                            name="department"
                            label="Department"
                            placeholder="Select department"
                            required
                            queryKey={[DEPARTMENT_ROUTES.INDEX]}
                            listFn={() => api.departments.list()}
                            mapOption={(item: any) => ({ value: String(item.id), label: item.name ?? String(item.id) })}
                            createForm={(props) => <DepartmentInlineForm {...props} />}
                            createLabel="Department"
                            {...STORE_OBJECT}
                        />
                        <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                            <RhfDateField name="delivery_date" label="Delivery Date" />
                            <RhfTimeField name="delivery_time" label="Delivery Time" />
                        </div>
                        <div className="flex justify-end gap-2">
                            <Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
                                Cancel
                            </Button>
                            <Button type="submit" disabled={form.formState.isSubmitting}>
                                {form.formState.isSubmitting ? "Checking in..." : "Check In"}
                            </Button>
                        </div>
                    </FieldGroup>
                </Rhform>
            </DialogContent>
        </Dialog>
    )
}
