"use client"

import { InsuranceTypeForm } from "@/modules/settings/insurance-types/insurance-type-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 { INSURANCE_TYPE_ROUTES } from "@garage/api"
import type { InsuranceTypesClient } from "@garage/api"

export default function InsuranceTypesPage() {
    return (
        <ResourcePage<InsuranceTypesClient>
            pageTitle="Insurance Types"
            routeKey={INSURANCE_TYPE_ROUTES.INDEX}
            searchable
            searchPlaceholder="Search insurance types..."
            getClient={(api) => api.insuranceTypes}
            headerProps={({ selectedItem, invalidateQuery }) => ({
                actions: (
                    <FormDialog title="Insurance Type">
                        {(resourceId) => (
                            <InsuranceTypeForm
                                resourceId={resourceId}
                                initialData={selectedItem}
                                onSuccess={invalidateQuery}
                            />
                        )}
                    </FormDialog>
                ),
            })}
            columns={({ actionsColumn }) => [
                {
                    accessorKey: "title",
                    header: ({ column }) => <ColumnHeader column={column} title="Title" />,
                    cell: ({ row }) => (row.original as any).title ?? (row.original as any).name ?? "—",
                },
                actionsColumn(),
            ]}
        />
    )
}