"use client"

import { AlertTriangle, Plus, 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,
    RhfAsyncSelectField,
    RhfCheckboxField,
} 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 {
    employeeFormSchema,
    type EmployeeFormValues,
    STATUS_OPTIONS,
    TYPE_OPTIONS,
    WAGE_TYPE_OPTIONS,
} from "./employee.schema"
import { EMPLOYEE_ROUTES, DEPARTMENT_ROUTES, SHOP_TIMING_ROUTES, SHOP_CALENDAR_ROUTES, GEO_ROUTES } from "@garage/api"

// ── Constants ──

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

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

const WAGE_TYPE_SELECT_OPTIONS = WAGE_TYPE_OPTIONS.map((v) => ({
    value: v,
    label: v,
}))

// ── Props ──

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

// ── Default values ──

const DEFAULT_VALUES: EmployeeFormValues = {
    department: null,
    country: null,
    shop_calender: null,
    shop_timing: null,
    role_id: null,
    first_name: "",
    last_name: "",
    email: "",
    password: "",
    phone: "",
    designation: "",
    salary: null,
    wage_type: null,
    status: "active",
    type: "employee",
    track_attendance: true,
    notify_owner_when_punch_in_out: false,
    geo_fence_radius: null,
}

// ── Mapping helpers ──

function mapToFormValues(data: unknown): EmployeeFormValues {
    const d = (data as any)?.data ?? data ?? {}

    return {
        department: toRelation(d.department_id, d.department?.name),
        country: toRelation(d.country_id, d.country?.name),
        shop_calender: toRelation(d.shop_calender_id, d.shop_calender?.title),
        shop_timing: toRelation(d.shop_timing_id, d.shop_timing?.title),
        role_id: d.role_id ? Number(d.role_id) : null,
        first_name: d.first_name || "",
        last_name: d.last_name || "",
        email: d.email || "",
        password: "",
        phone: d.phone || "",
        designation: d.designation || "",
        salary: d.salary ?? null,
        wage_type: d.wage_type ?? null,
        status: d.status || "active",
        type: d.type || "employee",
        track_attendance: d.track_attendance ?? true,
        notify_owner_when_punch_in_out: d.notify_owner_when_punch_in_out ?? false,
        geo_fence_radius: d.geo_fence_radius ?? null,
    }
}

function mapFormToPayload(values: EmployeeFormValues) {
    return {
        department_id: toId(values.department),
        country_id: toId(values.country),
        shop_calender_id: toId(values.shop_calender),
        shop_timing_id: toId(values.shop_timing),
        role_id: values.role_id ?? undefined,
        first_name: values.first_name,
        last_name: values.last_name,
        email: values.email,
        password: values.password,
        phone: values.phone || undefined,
        designation: values.designation || undefined,
        salary: values.salary ?? undefined,
        wage_type: values.wage_type ?? undefined,
        status: values.status,
        type: values.type,
        track_attendance: values.track_attendance,
        notify_owner_when_punch_in_out: values.notify_owner_when_punch_in_out,
        geo_fence_radius: values.geo_fence_radius ?? undefined,
    }
}

// ── Shared mapOption for async selects ──

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

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

// ── Component ──

export function EmployeeForm({ resourceId, initialData, onSuccess }: EmployeeFormProps) {
    const api = useAuthApi()

    const { form, isEditing } = useResourceForm<EmployeeFormValues, any>({
        schema: employeeFormSchema,
        defaultValues: DEFAULT_VALUES,
        resourceId,
        initialData,
        initialize: (id) => api.employees.show(id),
        queryKey: [EMPLOYEE_ROUTES.BY_ID, resourceId],
        mapToFormValues: mapToFormValues,
    })

    const { mutate, error, isPending } = useFormMutation(form, {
        mutationFn: (values: EmployeeFormValues) => {
            const payload = mapFormToPayload(values)
            const promise = isEditing && resourceId
                ? api.employees.update(resourceId, payload)
                : api.employees.create(payload)
            toast.promise(promise, {
                loading: isEditing ? "Updating employee..." : "Creating employee...",
                success: isEditing ? "Employee updated successfully" : "Employee created successfully",
                error: isEditing ? "Failed to update employee" : "Failed to create employee",
            })
            return promise
        },
        onSuccess: () => {
            form.reset()
            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>
                        {isEditing ? "Failed to update employee" : "Failed to create employee"}
                    </AlertTitle>
                    {error.message}
                </Alert>
            )}

            <FieldGroup>
                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <RhfTextField name="first_name" label="First Name" placeholder="Jane" required />
                    <RhfTextField name="last_name" label="Last Name" placeholder="Smith" required />
                </div>

                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <RhfTextField name="email" label="Email" placeholder="jane@example.com" type="email" />
                    <RhfTextField name="password" label="Password" placeholder="At least 6 characters" type="password" required />
                </div>

                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <RhfTextField name="phone" label="Phone" placeholder="0501234567" type="tel" />
                    <RhfTextField name="designation" label="Designation" placeholder="Technician" />
                </div>

                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <RhfAsyncSelectField
                        name="department"
                        label="Department"
                        placeholder="Select department"
                        queryKey={[DEPARTMENT_ROUTES.INDEX]}
                        listFn={() => api.departments.list()}
                        mapOption={mapLookupOption}
                        {...STORE_OBJECT}
                    />
                    <RhfTextField name="role_id" label="Role ID" placeholder="1" type="number" />
                </div>

                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <RhfSelectField
                        name="status"
                        label="Status"
                        placeholder="Select status"
                        options={STATUS_SELECT_OPTIONS}
                    />
                    <RhfSelectField
                        name="type"
                        label="Type"
                        placeholder="Select type"
                        options={TYPE_SELECT_OPTIONS}
                    />
                </div>

                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <RhfTextField name="salary" label="Salary" placeholder="0" type="number" />
                    <RhfSelectField
                        name="wage_type"
                        label="Wage Type"
                        placeholder="Select wage type"
                        options={WAGE_TYPE_SELECT_OPTIONS}
                    />
                </div>

                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <RhfAsyncSelectField
                        name="country"
                        label="Country"
                        placeholder="Select country"
                        queryKey={[GEO_ROUTES.COUNTRIES]}
                        listFn={() => api.geo.countries()}
                        mapOption={mapLookupOption}
                        {...STORE_OBJECT}
                    />
                    <RhfAsyncSelectField
                        name="shop_calender"
                        label="Shop Calendar"
                        placeholder="Select calendar"
                        queryKey={[SHOP_CALENDAR_ROUTES.INDEX]}
                        listFn={() => api.shopCalendars.list()}
                        mapOption={mapLookupOption}
                        {...STORE_OBJECT}
                    />
                    <RhfAsyncSelectField
                        name="shop_timing"
                        label="Shop Timing"
                        placeholder="Select timing"
                        queryKey={[SHOP_TIMING_ROUTES.INDEX]}
                        listFn={() => api.shopTimings.list()}
                        mapOption={mapLookupOption}
                        {...STORE_OBJECT}
                    />
                </div>

                <RhfTextField name="geo_fence_radius" label="Geo Fence Radius (m)" placeholder="100" type="number" />

                <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">
                    <RhfCheckboxField name="track_attendance" label="Track Attendance" />
                    <RhfCheckboxField name="notify_owner_when_punch_in_out" label="Notify Owner on Punch In/Out" />
                </div>

                <Button type="submit" variant="default" disabled={isPending}>
                    {isEditing ? <Save /> : <Plus />}
                    {isPending
                        ? (isEditing ? "Updating..." : "Creating...")
                        : (isEditing ? "Update Employee" : "Create Employee")}
                </Button>
            </FieldGroup>
        </Rhform>
    )
}
