import type { ColumnDef } from '@tanstack/vue-table';
import { h } from 'vue';
import { Badge } from '@/components/ui/badge';
import { DataTableColumnHeader } from '@/components/data-table';
import { stokOpnameStatusBadgeVariant } from '@/constants/stok-opname-status';
import type { StokOpnameListItem } from '@/types/stok-opname';
import DataTableActions from './data-table-actions.vue';
import { cn } from '@/lib/utils';

export function createColumns(
    onEdit: (item: StokOpnameListItem) => void,
    onSubmit: (item: StokOpnameListItem) => void,
    onVerify: (item: StokOpnameListItem) => void,
    onReject: (item: StokOpnameListItem) => void,
): ColumnDef<StokOpnameListItem>[] {
    return [
        {
            accessorKey: 'opname_date_formatted',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Tanggal', column: 'opname_date' }),
        },
        {
            accessorKey: 'status_label',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Status', column: 'status' }),
            cell: ({ row }) => h(Badge, {
                variant: stokOpnameStatusBadgeVariant(row.original.status),
                class: cn(row.original.status === 'verified' && 'bg-green-600 text-white'),
            }, () => row.original.status_label),
        },
        {
            accessorKey: 'items_count',
            enableSorting: false,
            header: () => 'Jumlah Item',
            cell: ({ row }) => `${row.original.items_count} item`,
        },
        {
            accessorKey: 'items_with_difference_count',
            enableSorting: false,
            header: () => 'Selisih',
            cell: ({ row }) => {
                const count = row.original.items_with_difference_count;
                if (count === 0) return '-';
                return h(Badge, { variant: 'destructive' }, () => `${count} selisih`);
            },
        },
        {
            accessorKey: 'created_by_name',
            enableSorting: false,
            header: () => 'Dibuat Oleh',
        },
        {
            accessorKey: 'created_at_formatted',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Waktu', column: 'created_at' }),
        },
        {
            id: 'actions',
            enableSorting: false,
            enableHiding: false,
            cell: ({ row }) => h(DataTableActions, {
                stokOpname: row.original,
                onEdit: () => onEdit(row.original),
                onSubmit: () => onSubmit(row.original),
                onVerify: () => onVerify(row.original),
                onReject: () => onReject(row.original),
            }),
        },
    ];
}
