import type { ColumnDef } from '@tanstack/vue-table';
import { h } from 'vue';
import { DataTableColumnHeader } from '@/components/data-table';
import type { AttendanceListItem } from '@/types/attendance';
import AttendancePhotoCell from './attendance-photo-cell.vue';
import DataTableActions from './data-table-actions.vue';

export function createColumns(canManageAll: boolean): ColumnDef<AttendanceListItem>[] {
    const columns: ColumnDef<AttendanceListItem>[] = [];

    if (canManageAll) {
        columns.push({
            accessorKey: 'employee_name',
            enableSorting: false,
            header: () => h(DataTableColumnHeader, { title: 'Pegawai', column: 'employee_name' }),
            cell: ({ row }) => row.original.employee_name ?? '-',
        });
    }

    columns.push(
        {
            accessorKey: 'attendance_date',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Tanggal', column: 'attendance_date' }),
            cell: ({ row }) => row.original.attendance_date_formatted,
        },
        {
            accessorKey: 'check_in_at',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Masuk', column: 'check_in_at' }),
            cell: ({ row }) => row.original.check_in_at_formatted ?? '-',
        },
        {
            accessorKey: 'check_out_at',
            enableSorting: true,
            header: () => h(DataTableColumnHeader, { title: 'Pulang', column: 'check_out_at' }),
            cell: ({ row }) => row.original.check_out_at_formatted ?? '-',
        },
        {
            id: 'duration',
            enableSorting: false,
            header: () => h(DataTableColumnHeader, { title: 'Durasi', column: 'duration' }),
            cell: ({ row }) => row.original.work_duration_formatted ?? '-',
        },
        {
            id: 'photos',
            enableSorting: false,
            header: () => h(DataTableColumnHeader, { title: 'Foto', column: 'photos' }),
            cell: ({ row }) => h(AttendancePhotoCell, {
                checkInPhotoUrl: row.original.check_in_photo_url,
                checkOutPhotoUrl: row.original.check_out_photo_url,
            }),
        },
        {
            id: 'actions',
            enableSorting: false,
            enableHiding: false,
            cell: ({ row }) => h(DataTableActions, {
                attendance: row.original,
            }),
        },
    );

    return columns;
}
