"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 { PurchaseOrderForm } from "@/modules/purchase-orders/purchase-order-form"
import { PURCHASE_ORDER_ROUTES } from "@garage/api"
import type { PurchaseOrdersClient } from "@garage/api"

export default function PurchaseOrdersPage() {
    return (
        <ResourcePage<PurchaseOrdersClient>
            pageTitle="Purchase Orders"
            routeKey={PURCHASE_ORDER_ROUTES.INDEX}
            getClient={(api) => api.purchaseOrders}
            headerProps={({ selectedItem, invalidateQuery }) => ({
                actions: (
                    <FormDialog title="Purchase Order">
                        {(resourceId) => (
                            <PurchaseOrderForm
                                resourceId={resourceId}
                                initialData={selectedItem}
                                onSuccess={invalidateQuery}
                            />
                        )}
                    </FormDialog>
                ),
            })}
            columns={({ actionsColumn }) => [
                {
                    accessorKey: "order_number",
                    header: ({ column }) => <ColumnHeader column={column} title="Order #" />,
                    cell: ({ row }) => (row.original as any).order_number || "—",
                },
                {
                    accessorKey: "title",
                    header: ({ column }) => <ColumnHeader column={column} title="Title" />,
                },
                {
                    accessorKey: "vendor_name",
                    header: ({ column }) => <ColumnHeader column={column} title="Vendor" />,
                    cell: ({ row }) => (row.original as any).vendor_name || "—",
                },
                {
                    accessorKey: "order_date",
                    header: ({ column }) => <ColumnHeader column={column} title="Order Date" />,
                    cell: ({ row }) => {
                        const val = (row.original as any).order_date
                        return val ? new Date(val).toLocaleDateString() : "—"
                    },
                },
                {
                    accessorKey: "delivery_date",
                    header: ({ column }) => <ColumnHeader column={column} title="Delivery Date" />,
                    cell: ({ row }) => {
                        const val = (row.original as any).delivery_date
                        return val ? new Date(val).toLocaleDateString() : "—"
                    },
                },
                {
                    accessorKey: "created_at",
                    header: ({ column }) => <ColumnHeader column={column} title="Created" />,
                    cell: ({ row }) => {
                        const val = (row.original as any).created_at
                        return val ? new Date(val).toLocaleDateString() : "—"
                    },
                },
                actionsColumn(),
            ]}
        />
    )
}
