import type { ColumnDef } from '@tanstack/vue-table';
import { h } from 'vue';
import { DataTableColumnHeader } from '@/components/data-table';
import { Badge } from '@/components/ui/badge';
import { employeeAdvanceStatusBadgeVariant } from '@/constants/employee-advance-status';
import type { EmployeeAdvanceListItem } from '@/types/employee-advance';
import DataTableActions from './data-table-actions.vue';

export function createColumns(
    authEmployeeId: number | null,
    onEdit: (employeeAdvance: EmployeeAdvanceListItem) => void,
    onReject: (employeeAdvance: EmployeeAdvanceListItem) => void,
): ColumnDef<EmployeeAdvanceListItem>[] {
    return [
        {
            accessorKey: 'created_at_formatted',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Tanggal', column: 'created_at' }),
        },
        {
            accessorKey: 'employee_name',
            enableSorting: false,
            header: () => 'Pegawai',
        },
        {
            accessorKey: 'amount_formatted',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Jumlah', column: 'amount' }),
        },
        {
            id: 'payment_status',
            enableSorting: false,
            header: () => 'Dibayar / Sisa',
            cell: ({ row }) => {
                const { paid_amount_formatted, remaining_amount, remaining_amount_formatted, status } = row.original;

                if (status === 'pending' || status === 'rejected') {
                    return h('span', { class: 'text-muted-foreground text-sm' }, '-');
                }

                return h('div', { class: 'space-y-0.5' }, [
                    h('div', { class: 'text-sm font-medium' }, paid_amount_formatted),
                    remaining_amount > 0
                        ? h('div', { class: 'text-xs text-muted-foreground' }, `Sisa: ${remaining_amount_formatted}`)
                        : h('div', { class: 'text-xs text-green-600 font-medium' }, 'Lunas'),
                ]);
            },
        },
        {
            accessorKey: 'description',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Keterangan', column: 'description' }),
        },

        {
            accessorKey: 'due_date_formatted',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Jatuh Tempo', column: 'due_date' }),
        },
        {
            accessorKey: 'status_label',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Status', column: 'status' }),
            cell: ({ row }) => h(
                Badge,
                { variant: employeeAdvanceStatusBadgeVariant(row.original.status) },
                () => row.original.status_label,
            ),
        },
        {
            accessorKey: 'rejection_reason',
            enableSorting: false,
            header: () => 'Alasan Penolakan',
            cell: ({ row }) => row.original.rejection_reason ?? '-',
        },
        {
            id: 'actions',
            enableSorting: false,
            enableHiding: false,
            cell: ({ row }) => h(DataTableActions, {
                employeeAdvance: row.original,
                authEmployeeId,
                onEdit: () => onEdit(row.original),
                onReject: () => onReject(row.original),
            }),
        },
    ];
}
