"use client"

import { AlertTriangle, Save } from "lucide-react"
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,
    RhfSelectField,
    RhfTextareaField,
    RhfDateField,
} 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 { toId } from "@/shared/lib/utils"
import { InvoiceStatus, InvoiceDiscount, INVOICE_ROUTES } from "@garage/api"
import { z } from "zod"

// ── Schema for edit form (simplified, edit only) ──

const invoiceEditFormSchema = z.object({
    subject: z.string().min(1, "Subject is required"),
    due_date: z.string().optional(),
    invoice_title: z.string().optional(),
    notes: z.string().optional(),
    terms_and_conditions: z.string().optional(),
    status: z.string().optional(),
    discount: z.string().optional(),
})

type InvoiceEditFormValues = z.infer<typeof invoiceEditFormSchema>

// ── Constants ──

const STATUS_OPTIONS = InvoiceStatus.map((v) => ({
    value: v,
    label: v.charAt(0).toUpperCase() + v.slice(1).replace(/_/g, " "),
}))

const DISCOUNT_OPTIONS = InvoiceDiscount.map((v) => ({
    value: v,
    label: v.charAt(0).toUpperCase() + v.slice(1).replace(/_/g, " "),
}))

// ── Default values ──

const DEFAULT_VALUES: InvoiceEditFormValues = {
    subject: "",
    due_date: "",
    invoice_title: "",
    notes: "",
    terms_and_conditions: "",
    status: "",
    discount: "no",
}

// ── Mapping helpers ──

function mapToFormValues(data: unknown): InvoiceEditFormValues {
    const d = (data as any)?.data ?? data ?? {}
    return {
        subject: d.subject || "",
        due_date: d.due_date || "",
        invoice_title: d.invoice_title || "",
        notes: d.notes || "",
        terms_and_conditions: d.terms_and_conditions || "",
        status: d.status || "",
        discount: d.discount || "no",
    }
}

function mapFormToPayload(values: InvoiceEditFormValues) {
    return {
        subject: values.subject,
        due_date: values.due_date || undefined,
        invoice_title: values.invoice_title || undefined,
        notes: values.notes || undefined,
        terms_and_conditions: values.terms_and_conditions || undefined,
        status: values.status || undefined,
        discount: values.discount || "no",
    }
}

// ── Props ──

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

// ── Component ──

export function InvoiceEditForm({ resourceId, initialData, onSuccess }: InvoiceEditFormProps) {
    const api = useAuthApi()

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

    const { mutate, error, isPending } = useFormMutation(form, {
        mutationFn: (values: InvoiceEditFormValues) => {
            const payload = mapFormToPayload(values)
            const promise = api.invoices.update(resourceId, payload as any)
            toast.promise(promise, {
                loading: "Updating invoice...",
                success: "Invoice updated successfully",
                error: "Failed to update invoice",
            })
            return promise
        },
        onSuccess: () => {
            onSuccess?.()
        },
    })

    return (
        <Rhform form={form} onSubmit={(values) => mutate(values)}>
            {error && (
                <Alert variant="destructive" className="mb-4">
                    <AlertTriangle className="me-2 h-4 w-4" />
                    <AlertTitle>Failed to update invoice</AlertTitle>
                    {error.message}
                </Alert>
            )}

            <FieldGroup>
                <RhfTextField
                    name="subject"
                    label="Subject"
                    placeholder="Invoice subject"
                    required
                />

                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <RhfDateField
                        name="due_date"
                        label="Due Date"
                    />

                    <RhfSelectField
                        name="status"
                        label="Status"
                        placeholder="Select status"
                        options={STATUS_OPTIONS}
                    />
                </div>

                <RhfTextField
                    name="invoice_title"
                    label="Invoice Title"
                    placeholder="e.g., Tax Invoice, Proforma Invoice"
                />

                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <RhfSelectField
                        name="discount"
                        label="Discount Type"
                        placeholder="Select discount type"
                        options={DISCOUNT_OPTIONS}
                    />
                </div>

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

                <RhfTextareaField
                    name="terms_and_conditions"
                    label="Terms & Conditions"
                    rows={3}
                    placeholder="Add terms and conditions..."
                />

                <Button type="submit" variant="default" disabled={isPending}>
                    <Save className="size-4" />
                    {isPending ? "Updating..." : "Update Invoice"}
                </Button>
            </FieldGroup>
        </Rhform>
    )
}
