"use client"

import { useRouter } from 'next/navigation'
import { ResourcePage } from '@/shared/data-view/resource-page'
import { ColumnHeader } from '@/shared/data-view/table-view'
import type { ColumnDef } from '@tanstack/react-table'
import FormDialog from '@/shared/components/form-dialog'
import { ImportDataButton } from '@/shared/components/import-data-button'
import { ExportDataButton } from '@/shared/components/export-data-button'
import { useAuthApi } from '@/shared/useApi'
import type { CrudResourceColumnHelpers, ResourceItem } from '@/shared/data-view/resource-page'
import { VehicleForm } from '@/modules/vehicles/vehicle-form'
import { VEHICLE_ROUTES } from '@garage/api'
import type { VehiclesClient } from '@garage/api'
import { CarIcon, UserIcon } from 'lucide-react'
import { getFullName } from '@/shared/utils/getFullName'
import { RelationLink } from '@/shared/components/relation-link'

export default function VehiclesPage() {
    const router = useRouter()
    const api = useAuthApi()
    return (
        <ResourcePage<VehiclesClient>
            pageTitle="Vehicles"
            routeKey={VEHICLE_ROUTES.INDEX}
            searchable
            searchPlaceholder="Search vehicles..."
            getClient={(api) => api.vehicles}
            onRowClick={(row) => router.push(`/sales/vehicles/${(row as any).id}`)}
            headerProps={({ selectedItem, invalidateQuery }) => ({
                actions: (
                    <div className='flex items-center gap-2'>
                        <ImportDataButton
                            onImport={(file) => api.vehicles.importData(file)}
                            onSuccess={invalidateQuery}
                            entityLabel="Vehicles"
                            onDownloadSample={() => api.vehicles.downloadImportSample()}
                            sampleFileName='vehicles-import-sample'
                        />
                        <ExportDataButton
                            onExport={(filters) => api.vehicles.exportData(filters)}
                            fileName='vehicles'
                        />
                        <FormDialog title="Vehicle" classNames={{ dialogContent: 'lg:min-w-4xl' }}>
                            {(resourceId) => (
                                <VehicleForm
                                    resourceId={resourceId}
                                    initialData={selectedItem}
                                    onSuccess={invalidateQuery}
                                />
                            )}
                        </FormDialog>
                    </div>
                ),
            })}

            columns={({ actionsColumn }) => [
                {
                    id: "name",

                    header: ({ column }) => <ColumnHeader column={column} title="Vehicle" />,
                    cell: ({ row }) => {
                        const r = row.original as any
                        const make = r.make ?? ""
                        const model = r.model ?? ""
                        const display = r.name || `${make} ${model}`.trim() || "—"
                        return (
                            <div className="flex items-center gap-2">
                                <CarIcon className="h-4 w-4 text-muted-foreground" />
                                <div>
                                    <span className="font-medium">{display}</span>
                                    {r.sub_model && (
                                        <span className="ml-1 text-xs text-muted-foreground">{r.sub_model}</span>
                                    )}
                                </div>
                            </div>
                        )
                    },
                },
                {
                    accessorKey: "customers",
                    header: ({ column }: any) => <ColumnHeader column={column} title="Customer" />,
                    cell: ({ row }: any) => {
                        const customer = (row.original as any).customers?.[0]
                        return (
                            <RelationLink
                                href={customer?.id ? `/sales/customers/${customer.id}` : null}
                                icon={UserIcon}
                                label={getFullName(customer)}
                            />
                        )
                    },
                } as any,
                {
                    accessorKey: "year",
                    header: ({ column }) => <ColumnHeader column={column} title="Year" />,
                    cell: ({ row }) => (row.original as any).year ?? "—",
                },
                {
                    accessorKey: "license_plate",
                    header: ({ column }) => <ColumnHeader column={column} title="License Plate" />,
                    cell: ({ row }) => {
                        const val = (row.original as any).license_plate
                        return val
                            ? <span className="font-mono text-xs">{val}</span>
                            : "—"
                    },
                },
                {
                    accessorKey: "vin_number",
                    header: ({ column }) => <ColumnHeader column={column} title="VIN" />,
                    cell: ({ row }) => {
                        const val = (row.original as any).vin_number
                        return val
                            ? <span className="max-w-30 truncate block font-mono text-xs">{val}</span>
                            : "—"
                    },
                },
                {
                    accessorKey: "engine_size",
                    header: ({ column }) => <ColumnHeader column={column} title="Engine" />,
                    cell: ({ row }) => (row.original as any).engine_size ?? "—",
                },
                {
                    accessorKey: "mileage",
                    header: ({ column }) => <ColumnHeader column={column} title="Mileage" />,
                    cell: ({ row }) => {
                        const val = (row.original as any).mileage
                        return val != null ? `${Number(val).toLocaleString()} mi` : "—"
                    },
                },

                actionsColumn(),
            ]}
        />
    )
}