"use client"

import { AlertTriangle, Plus, Save, Trash2 } from "lucide-react"
import { useFieldArray } from "react-hook-form"

import { Button } from "@/shared/components/ui/button"
import { Alert, AlertTitle } from "@/shared/components/ui/alert"
import { FieldGroup } from "@/shared/components/ui/field"
import {
    Rhform,
    RhfTextField,
    RhfTextareaField,
    RhfAsyncSelectField,
} from "@/shared/components/form"
import { toast } from "sonner"
import { useAuthApi } from "@/shared/useApi"
import { useResourceForm } from "@/shared/hooks/use-resource-form"
import { useFormMutation } from "@/shared/hooks/use-form-mutation"
import { toRelation, toId } from "@/shared/lib/utils"

import {
    inventoryAdjustmentFormSchema,
    type InventoryAdjustmentFormValues,
} from "./inventory-adjustment.schema"
import { INVENTORY_ADJUSTMENT_ROUTES, REASON_ROUTES, PARTS_ROUTES } from "@garage/api"

// ── Props ──

export type InventoryAdjustmentFormProps = {
    resourceId?: string | null
    initialData?: unknown
    onSuccess?: () => void
}

// ── Default values ──

const DEFAULT_VALUES: InventoryAdjustmentFormValues = {
    reference_number: "",
    date: "",
    chart_of_account: "",
    reason: null,
    notes: "",
    parts: [{ part: null, quantity: 1, rate: 0 }],
}

// ── Mapping helpers ──

function mapToFormValues(data: unknown): InventoryAdjustmentFormValues {
    const d = (data as any)?.data ?? data ?? {}
    return {
        reference_number: d.reference_number || "",
        date: d.date || "",
        chart_of_account: d.chart_of_account || "",
        reason: toRelation(d.reason_id, d.reason_name),
        notes: d.notes || "",
        parts: Array.isArray(d.parts) && d.parts.length > 0
            ? d.parts.map((p: any) => ({
                part: toRelation(p.part_id, p.part_name),
                quantity: p.quantity ?? 1,
                rate: p.rate ?? 0,
            }))
            : [{ part: null, quantity: 1, rate: 0 }],
    }
}

function mapFormToPayload(values: InventoryAdjustmentFormValues) {
    return {
        reference_number: values.reference_number || undefined,
        date: values.date || undefined,
        chart_of_account: values.chart_of_account || undefined,
        reason_id: toId(values.reason) ? Number(toId(values.reason)) : undefined,
        notes: values.notes || undefined,
        parts: values.parts.map((p) => ({
            part_id: toId(p.part) ? Number(toId(p.part)) : undefined,
            quantity: p.quantity,
            rate: p.rate,
        })),
    }
}

const mapLookupOption = (item: any) => ({ value: String(item.id), label: item.name ?? item.title })
const STORE_OBJECT = { getOptionValue: (o: any) => o, getOptionLabel: (o: any) => o.label }

// ── Component ──

export function InventoryAdjustmentForm({ resourceId, initialData, onSuccess }: InventoryAdjustmentFormProps) {
    const api = useAuthApi()

    const { form, isEditing } = useResourceForm<InventoryAdjustmentFormValues, any>({
        schema: inventoryAdjustmentFormSchema,
        defaultValues: DEFAULT_VALUES,
        resourceId,
        initialData,
        mapToFormValues,
    })

    const { fields, append, remove } = useFieldArray({
        control: form.control,
        name: "parts",
    })

    const { mutate, error, isPending } = useFormMutation(form, {
        mutationFn: (values: InventoryAdjustmentFormValues) => {
            const payload = mapFormToPayload(values)
            const promise = isEditing && resourceId
                ? api.inventoryAdjustments.update(resourceId, payload as never)
                : api.inventoryAdjustments.create(payload as never)
            return promise
        },
        onSuccess: () => {
            toast.success(isEditing ? "Adjustment updated." : "Adjustment created.")
            onSuccess?.()
        },
    })

    return (
        <Rhform form={form} onSubmit={mutate} className="flex flex-col gap-4">
            {error && (
                <Alert variant="destructive">
                    <AlertTriangle className="size-4" />
                    <AlertTitle>
                        {isEditing ? "Failed to update adjustment" : "Failed to create adjustment"}
                    </AlertTitle>
                    {error.message}
                </Alert>
            )}

            <FieldGroup>
                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <RhfTextField
                        name="reference_number"
                        label="Reference Number"
                        placeholder="ADJ-001"
                    />
                    <RhfTextField
                        name="date"
                        label="Date"
                        type="date"
                    />
                    <RhfTextField
                        name="chart_of_account"
                        label="Chart of Account"
                        placeholder="Account name"
                    />
                    <RhfAsyncSelectField
                        name="reason"
                        label="Reason"
                        placeholder="Select reason..."
                        queryKey={[REASON_ROUTES.INDEX]}
                        listFn={() => api.reasons.list()}
                        mapOption={mapLookupOption}
                        {...STORE_OBJECT}
                    />
                </div>

                <RhfTextareaField
                    name="notes"
                    label="Notes"
                    placeholder="Additional notes..."
                    rows={3}
                />
            </FieldGroup>

            <div className="flex flex-col gap-2">
                <div className="flex items-center justify-between">
                    <span className="text-sm font-medium">Parts</span>
                    <Button
                        type="button"
                        variant="outline"
                        size="sm"
                        onClick={() => append({ part: null, quantity: 1, rate: 0 })}
                    >
                        <Plus className="size-4" />
                        Add Part
                    </Button>
                </div>

                {fields.length === 0 && (
                    <p className="text-sm text-muted-foreground">No parts added. Click "Add Part" to begin.</p>
                )}

                {fields.map((field, index) => (
                    <div key={field.id} className="grid grid-cols-[1fr_auto_auto_auto] items-end gap-2 rounded-lg border p-3">
                        <RhfAsyncSelectField
                            name={`parts.${index}.part`}
                            label="Part"
                            placeholder="Select part..."
                            queryKey={[PARTS_ROUTES.INDEX]}
                            listFn={() => api.parts.list()}
                            mapOption={mapLookupOption}
                            {...STORE_OBJECT}
                        />
                        <RhfTextField
                            name={`parts.${index}.quantity`}
                            label="Qty"
                            type="number"
                            placeholder="1"
                        />
                        <RhfTextField
                            name={`parts.${index}.rate`}
                            label="Rate"
                            type="number"
                            placeholder="0"
                        />
                        <Button
                            type="button"
                            variant="ghost"
                            size="icon"
                            className="mb-0.5"
                            onClick={() => remove(index)}
                            disabled={fields.length === 1}
                        >
                            <Trash2 className="size-4 text-destructive" />
                        </Button>
                    </div>
                ))}
            </div>

            <div className="flex justify-end">
                <Button type="submit" disabled={isPending}>
                    <Save className="size-4" />
                    {isPending ? "Saving..." : isEditing ? "Update Adjustment" : "Create Adjustment"}
                </Button>
            </div>
        </Rhform>
    )
}
