"use client"

import { ResourcePage } from '@/shared/data-view/resource-page'
import { ColumnHeader } from '@/shared/data-view/table-view'
import { VehicleForm } from '@/modules/vehicles/vehicle-form'
import { VEHICLE_ROUTES } from '@repo/api'
import type { VehiclesClient } from '@repo/api'
import { CarIcon } from 'lucide-react'

export default function VehiclesPage() {
    return (
        <ResourcePage<VehiclesClient>
            pageTitle="Vehicles"
            title="Vehicle"
            routeKey={VEHICLE_ROUTES.INDEX}
            getClient={(api) => api.vehicles}
            columns={({ actionsColumn }) => [
                {
                    accessorKey: "name",
                    header: ({ column }) => <ColumnHeader column={column} title="Vehicle" />,
                    cell: ({ row }) => {
                        const r = row.original as any
                        const display = r.name || `${r.make ?? ""} ${r.model ?? ""}`.trim() || "—"
                        return (
                            <div className="flex items-center gap-2">
                                <CarIcon className="h-4 w-4 text-muted-foreground" />
                                <span>{display}</span>
                            </div>
                        )
                    },
                },
                {
                    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 }) => (row.original as any).license_plate ?? "—",
                },
                {
                    accessorKey: "mileage",
                    header: ({ column }) => <ColumnHeader column={column} title="Mileage" />,
                    cell: ({ row }) => (row.original as any).mileage ?? "—",
                },
                actionsColumn(),
            ]}
            renderForm={({ resourceId, initialData, onSuccess }) => (
                <VehicleForm
                    resourceId={resourceId}
                    initialData={initialData}
                    onSuccess={onSuccess}
                />
            )}
        />
    )
}