/**
 * Definición de columnas para la tabla de Conductores
 */
import type { Driver } from '$lib/api/dashboard/a76/drivers';
import type { ColumnDef } from '@tanstack/table-core';
import { renderComponent } from '$lib/components/ui/data-table';
import DataTableActions from './data-table-actions.svelte';

export function createColumns(
	onSuccess: () => void,
	permissions: { canEdit: boolean; canDelete: boolean } = { canEdit: true, canDelete: true }
): ColumnDef<Driver>[] {
	const cols: ColumnDef<Driver>[] = [
		{
			accessorKey: 'transporter_key',
			header: 'Clave Transportista',
			cell: ({ row }) => row.original.transporter_key
		},
		{
			accessorKey: 'line',
			header: 'Línea',
			cell: ({ row }) => row.original.line
		},
		{
			accessorKey: 'driver_name',
			header: 'Nombre del Conductor',
			cell: ({ row }) => row.original.driver_name || '-'
		},
		{
			accessorKey: 'license_number',
			header: 'Número de Licencia',
			cell: ({ row }) => row.original.license_number || '-'
		},
		{
			accessorKey: 'first_name',
			header: 'Nombre',
			cell: ({ row }) => row.original.first_name || '-'
		},
		{
			accessorKey: 'last_name',
			header: 'Apellido',
			cell: ({ row }) => row.original.last_name || '-'
		}
	];
	if (permissions.canEdit || permissions.canDelete) {
		cols.push({
			id: 'actions',
			header: 'Acciones',
			cell: ({ row }) => {
				return renderComponent(DataTableActions, {
					item: row.original,
					onSuccess,
					canEdit: permissions.canEdit,
					canDelete: permissions.canDelete
				});
			}
		});
	}
	return cols;
}
