import { ColumnHeader } from "@/shared/data-view/table-view"
import { Money } from "@/shared/components/money"
import { Badge } from "@/shared/components/ui/badge"
import type { ColumnDef } from "@tanstack/react-table"

/** Core part columns shared between the parts page and selector dialogs. */
export const partColumns = {
    title: {
        accessorKey: "title",
        header: ({ column }) => <ColumnHeader column={column} title="Name" />,
        cell: ({ row }) => {
            const r = row.original as any
            return (
                <div>
                    <span className="font-medium">{r.title || "—"}</span>
                    {r.sku && <span className="ml-2 text-xs text-muted-foreground">{r.sku}</span>}
                </div>
            )
        },
    },
    partNumber: {
        accessorKey: "part_number",
        header: ({ column }) => <ColumnHeader column={column} title="Part #" />,
        cell: ({ row }) => (row.original as any).part_number || "—",
    },
    manufacturer: {
        accessorKey: "manufactured_by",
        header: ({ column }) => <ColumnHeader column={column} title="Manufacturer" />,
        cell: ({ row }) => (row.original as any).manufactured_by || "—",
    },
    sellingPrice: {
        accessorKey: "selling_price",
        header: ({ column }) => <ColumnHeader column={column} title="Sell Price" />,
        cell: ({ row }) => {
            const val = (row.original as any).selling_price
            return val != null ? <Money value={val} /> : "—"
        },
    },
    purchasePrice: {
        accessorKey: "purchase_price",
        header: ({ column }) => <ColumnHeader column={column} title="Purchase Price" />,
        cell: ({ row }) => {
            const val = (row.original as any).purchase_price
            return val != null ? <Money value={val} /> : "—"
        },
    },
    stock: {
        accessorKey: "available_stock",
        header: ({ column }) => <ColumnHeader column={column} title="Stock" />,
        cell: ({ row }) => (row.original as any).available_stock ?? "—",
    },
    status: {
        accessorKey: "is_active",
        header: ({ column }) => <ColumnHeader column={column} title="Status" />,
        cell: ({ row }) => {
            const active = (row.original as any).is_active
            return (
                <Badge variant={active ? "default" : "secondary"}>
                    {active ? "Active" : "Inactive"}
                </Badge>
            )
        },
    },
    createdAt: {
        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() : "—"
        },
    },
} satisfies Record<string, ColumnDef<any, any>>
