"use client"

import { MakeAndModelForm } from "@/modules/settings/make-and-models/make-and-model-form"
import { ResourcePage } from "@/shared/data-view/resource-page"
import { ColumnHeader } from "@/shared/data-view/table-view"
import FormDialog from "@/shared/components/form-dialog"
import { MAKE_AND_MODEL_ROUTES } from "@garage/api"
import type { MakeAndModelsClient } from "@garage/api"

export default function MakeAndModelsPage() {
    return (
        <ResourcePage<MakeAndModelsClient>
            pageTitle="Make & Models"
            routeKey={MAKE_AND_MODEL_ROUTES.INDEX}
            searchable
            searchPlaceholder="Search makes & models..."
            getClient={(api) => api.makeAndModels}
            headerProps={({ selectedItem, invalidateQuery }) => ({
                actions: (
                    <FormDialog title="Make & Model">
                        {(resourceId) => (
                            <MakeAndModelForm
                                resourceId={resourceId}
                                initialData={selectedItem}
                                onSuccess={invalidateQuery}
                            />
                        )}
                    </FormDialog>
                ),
            })}
            columns={({ actionsColumn }) => [
                {
                    accessorKey: "make",
                    header: ({ column }) => <ColumnHeader column={column} title="Make" />,
                    cell: ({ row }) => (row.original as any).make ?? "—",
                },
                {
                    accessorKey: "model",
                    header: ({ column }) => <ColumnHeader column={column} title="Model" />,
                    cell: ({ row }) => (row.original as any).model ?? "—",
                },
                {
                    accessorKey: "year",
                    header: ({ column }) => <ColumnHeader column={column} title="Year" />,
                    cell: ({ row }) => (row.original as any).year ?? "—",
                },
                {
                    accessorKey: "sub_model",
                    header: ({ column }) => <ColumnHeader column={column} title="Sub Model" />,
                    cell: ({ row }) => (row.original as any).sub_model ?? "—",
                },
                {
                    accessorKey: "engine_size",
                    header: ({ column }) => <ColumnHeader column={column} title="Engine Size" />,
                    cell: ({ row }) => (row.original as any).engine_size ?? "—",
                },
                {
                    accessorKey: "drivetrain",
                    header: ({ column }) => <ColumnHeader column={column} title="Drivetrain" />,
                    cell: ({ row }) => (row.original as any).drivetrain ?? "—",
                },
                {
                    accessorKey: "is_active",
                    header: ({ column }) => <ColumnHeader column={column} title="Status" />,
                    cell: ({ row }) => {
                        const active = (row.original as any).is_active
                        return (
                            <span className={active ? "text-green-600" : "text-muted-foreground"}>
                                {active ? "Active" : "Inactive"}
                            </span>
                        )
                    },
                },
                actionsColumn(),
            ]}
        />
    )
}
