"use client"

import { useRouter } from "next/navigation"
import FormDialog from "@/shared/components/form-dialog"
import { Badge } from "@/shared/components/ui/badge"
import { ResourcePage } from "@/shared/data-view/resource-page"
import { ColumnHeader } from "@/shared/data-view/table-view"
import { BillForm } from "@/modules/bills/bill-form"
import { BILL_ROUTES } from "@garage/api"
import type { BillsClient } from "@garage/api"
import { formatDate } from "@/shared/utils/formatters"

export default function BillsPage() {
    const router = useRouter()

    return (
        <ResourcePage<BillsClient>
            pageTitle="Bills"
            routeKey={BILL_ROUTES.INDEX}
            getClient={(api) => api.bills}
            onRowClick={(row) => router.push(`/purchase/bill/${(row as any).id}`)}
            headerProps={({ selectedItem, invalidateQuery }) => ({
                actions: (
                    <FormDialog classNames={{ dialogContent: "lg:min-w-6xl" }} title="Bill">
                        {(resourceId) => (
                            <BillForm
                                resourceId={resourceId}
                                initialData={selectedItem}
                                onSuccess={invalidateQuery}
                            />
                        )}
                    </FormDialog>
                ),
            })}
            columns={({ actionsColumn }) => [
                {
                    accessorKey: "bill_number",
                    header: ({ column }) => <ColumnHeader column={column} title="Bill #" />,
                    cell: ({ row }) => (row.original as any).bill_number || "—",
                },
                {
                    accessorKey: "total",
                    header: ({ column }) => <ColumnHeader column={column} title="Total" />,
                    cell: ({ row }) => (row.original as any).total || "—",
                },
                {
                    accessorKey: "balance_due",
                    header: ({ column }) => <ColumnHeader column={column} title="Balance Due" />,
                    cell: ({ row }) => (row.original as any).balance_due || "—",
                },
                {
                    accessorKey: "title",
                    header: ({ column }) => <ColumnHeader column={column} title="Title" />,
                },
                {
                    accessorKey: "vendor",
                    header: ({ column }) => <ColumnHeader column={column} title="Vendor" />,
                    cell: ({ row }) => (row.original as any).vendor?.name || "—",
                },
                {
                    accessorKey: "bill_date",
                    header: ({ column }) => <ColumnHeader column={column} title="Bill Date" />,
                    cell: ({ row }) => formatDate((row.original as any).bill_date) || "—",
                },
                {
                    accessorKey: "bill_due_date",
                    header: ({ column }) => <ColumnHeader column={column} title="Due Date" />,
                    cell: ({ row }) => formatDate((row.original as any).bill_due_date) || "—",
                },
                {
                    accessorKey: "status",
                    header: ({ column }) => <ColumnHeader column={column} title="Status" />,
                    cell: ({ row }) => {
                        const status = (row.original as any).status
                        return (
                            <Badge variant={status === "paid" ? "default" : "secondary"}>
                                {status?.replace(/_/g, " ") || "—"}
                            </Badge>
                        )
                    },
                },
                actionsColumn(),
            ]}
        />
    )
}

