"use client"

import { ResourcePage } from "@/shared/data-view/resource-page"
import { ColumnHeader } from "@/shared/data-view/table-view"
import FormDialog from "@/shared/components/form-dialog"
import { InspectionForm } from "@/modules/inspections/inspection-form"
import { INSPECTION_ROUTES, InspectionStatus } from "@garage/api"
import type { InspectionsClient } from "@garage/api"
import { useRouter } from "next/navigation"
import { useJobCard } from "@/modules/job-cards/job-card-context"
import { RelationLink } from "@/shared/components/relation-link"
import { Car, UserIcon } from "lucide-react"
import { getFullName } from "@/shared/utils/getFullName"
import { getVehicleLabel } from "@/modules/vehicles/utils/getVehicleLabel"

export default function InspectionsPage() {
    const router = useRouter()
    const jobCard = useJobCard()
    return (
        <ResourcePage<InspectionsClient>
            pageTitle="Inspections"
            extraParams={{job_card_id: jobCard?.id}}
            routeKey={INSPECTION_ROUTES.INDEX}
            searchable
            searchPlaceholder="Search inspections..."
            statusFilter={{ statuses: InspectionStatus }}
            getClient={(api) => api.inspections}
            onRowClick={(row) => router.push(`/sales/inspections/${(row as any).id}`)}
            headerProps={({ selectedItem, invalidateQuery }) => ({
                actions: (
                    <FormDialog title="Inspection">
                        {(resourceId) => (
                            <InspectionForm
                                resourceId={resourceId}
                                initialData={selectedItem ?? {job_card: {value: jobCard?.id, label: jobCard?.title || `Job Card #${jobCard?.id}`}}}
                                onSuccess={invalidateQuery}
                            />
                        )}
                    </FormDialog>
                ),
            })}
            columns={({ actionsColumn }) => [
                {
                    accessorKey: "title",
                    header: ({ column }) => <ColumnHeader column={column} title="Title" />,
                },
                {
                    accessorKey: "customer",
                    header: ({ column }) => <ColumnHeader column={column} title="Customer" />,
                    cell: ({ row }) => {
                        const c = (row.original as any).customer
                        return (
                            <RelationLink
                                href={c?.id ? `/sales/customers/${c.id}` : null}
                                icon={UserIcon}
                                label={getFullName(c)}
                            />
                        )
                    },
                },
                {
                    accessorKey: "vehicle",
                    header: ({ column }) => <ColumnHeader column={column} title="Vehicle" />,
                    cell: ({ row }) => {
                        const v = (row.original as any).vehicle
                        return (
                            <RelationLink
                                href={v?.id ? `/sales/vehicles/${v.id}` : null}
                                icon={Car}
                                label={getVehicleLabel(v as any)}
                                meta={v?.license_plate}
                            />
                        )
                    },
                },
                {
                    accessorKey: "inspection_category",
                    header: ({ column }) => <ColumnHeader column={column} title="Category" />,
                    cell: ({ row }) => (row.original as any).inspection_category?.name ?? "—",
                },
                {
                    accessorKey: "status",
                    header: ({ column }) => <ColumnHeader column={column} title="Status" />,
                    cell: ({ row }) => {
                        const status = (row.original as any).status
                        return (
                            <span className={status === "completed" ? "text-green-600" : "text-yellow-600"}>
                                {status ?? "—"}
                            </span>
                        )
                    },
                },
                actionsColumn(),
            ]}
        />
    )
}
