"use client"

import { z } from "zod"
import { useForm, useFormContext } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { Plus } from "lucide-react"
import { Button } from "@/shared/components/ui/button"
import { FieldGroup } from "@/shared/components/ui/field"
import { Rhform, RhfTextField, type InlineCreateFormProps } from "@/shared/components/form"
import { toast } from "sonner"
import { useAuthApi } from "@/shared/useApi"

const schema = z.object({
    title: z.string().min(1, "Title is required").max(50, "Title cannot exceed 50 characters"),
})

type FormValues = z.infer<typeof schema>

export function BodyTypeInlineForm({ onSuccess }: InlineCreateFormProps) {
    const api = useAuthApi()
    const parentForm = useFormContext()

    const form = useForm<FormValues>({
        resolver: zodResolver(schema),
        defaultValues: { title: "" },
    })

    const handleSubmit = async (values: FormValues) => {
        const parentShopType = parentForm?.getValues("shop_type_id") as { value?: string } | null | undefined
        const shopTypeId = parentShopType?.value ? Number(parentShopType.value) : undefined

        if (!shopTypeId) {
            toast.error("Select a shop type before creating a body type")
            return
        }

        try {
            const result = await api.vehicleAttributes.createBodyType({ title: values.title, shop_type_id: shopTypeId } as any)
            toast.success("Body type created")
            form.reset()
            const item = (result as any)?.data ?? result
            onSuccess({ value: String(item.id), label: item.title ?? String(item.id) })
        } catch {
            toast.error("Failed to create body type")
        }
    }

    return (
        <Rhform form={form} onSubmit={handleSubmit}>
            <FieldGroup>
                <RhfTextField
                    name="title"
                    label="Title"
                    placeholder="e.g. Sedan"
                    required
                />
                <Button type="submit" disabled={form.formState.isSubmitting}>
                    <Plus />
                    {form.formState.isSubmitting ? "Creating..." : "Create Body Type"}
                </Button>
            </FieldGroup>
        </Rhform>
    )
}
