import type { ColumnDef } from '@tanstack/vue-table';
import { h } from 'vue';
import { DataTableColumnHeader } from '@/components/data-table';
import { Badge } from '@/components/ui/badge';
import { leaveRequestStatusBadgeVariant } from '@/constants/leave-request-status';
import type { LeaveRequestListItem } from '@/types/leave-request';
import DataTableActions from './data-table-actions.vue';

export function createColumns(
    authEmployeeId: number | null,
    onEdit: (leaveRequest: LeaveRequestListItem) => void,
    onReject: (leaveRequest: LeaveRequestListItem) => void,
): ColumnDef<LeaveRequestListItem>[] {
    return [
        {
            accessorKey: 'created_at_formatted',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Tanggal Pengajuan', column: 'created_at' }),
        },
        {
            accessorKey: 'employee_name',
            enableSorting: false,
            header: () => 'Pegawai',
        },
        {
            accessorKey: 'start_date_formatted',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Mulai', column: 'start_date' }),
        },
        {
            accessorKey: 'end_date_formatted',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Selesai', column: 'end_date' }),
        },
        {
            accessorKey: 'total_days',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Hari', column: 'total_days' }),
            cell: ({ row }) => `${row.original.total_days} hari`,
        },
        {
            accessorKey: 'status_label',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Status', column: 'status' }),
            cell: ({ row }) => h(
                Badge,
                { variant: leaveRequestStatusBadgeVariant(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, {
                leaveRequest: row.original,
                authEmployeeId,
                onEdit: () => onEdit(row.original),
                onReject: () => onReject(row.original),
            }),
        },
    ];
}
