"use client"

import { use } from "react"
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"
import { useJobCard } from "@/modules/job-cards/job-card-context"
import { RelationLink } from "@/shared/components/relation-link"
import { Building2, FileTextIcon } from "lucide-react"
import { getFullName } from "@/shared/utils/getFullName"

export default function JobCardPurchaseOrdersPage({
    params,
}: {
    params: Promise<{ id: string }>
}) {
    const { id: jobCardId } = use(params)
    const jobCard = useJobCard()

    const defaultJobCard = jobCard
        ? { value: String((jobCard as any).id), label: (jobCard as any).label || (jobCard as any).title || `Job Card` }
        : null

    return (
        <ResourcePage<PurchaseOrdersClient>
            routeKey={PURCHASE_ORDER_ROUTES.INDEX}
            searchable
            searchPlaceholder="Search purchase orders..."
            getClient={(api) => api.purchaseOrders}
            extraParams={{ job_card_id: jobCardId }}
            header={null}
            tableHeader={({ invalidateQuery, selectedItem, closeDialog }) => (
                <div className="flex justify-end">

                <FormDialog title="Purchase Order">
                    {(resourceId) => (
                        <PurchaseOrderForm
                            resourceId={resourceId}
                            initialData={selectedItem ?? { job_card: defaultJobCard }}
                            onSuccess={() => { closeDialog(); invalidateQuery() }}
                        />
                    )}
                </FormDialog>
                </div>

            )}
            columns={({ actionsColumn }) => [
                {
                    accessorKey: "order_number",
                    header: ({ column }) => <ColumnHeader column={column} title="Order #" />,
                    cell: ({ row }) => {
                        const item = row.original as any
                        return (
                            <RelationLink
                                href={item.id ? `/purchase/purchase-order/${item.id}` : null}
                                icon={FileTextIcon}
                                label={item.order_number}
                            />
                        )
                    },
                },
                {
                    accessorKey: "title",
                    header: ({ column }) => <ColumnHeader column={column} title="Title" />,
                },
                {
                    accessorKey: "vendor_name",
                    header: ({ column }) => <ColumnHeader column={column} title="Vendor" />,
                    cell: ({ row }) => {
                        const item = row.original as any
                        return (
                            <RelationLink
                                href={item.vendor?.id ? `/purchase/vendor/${item.vendor.id}` : null}
                                icon={Building2}
                                label={item.vendor?.company_name || getFullName(item.vendor) || item.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(),
            ]}
        />
    )
}
