/**
 * Definición de columnas para la tabla de Vehículos
 */
import type { Vehicle } from '$lib/api/dashboard/a76/vehicles';
import type { ColumnDef } from '@tanstack/table-core';
import { renderComponent } from '$lib/components/ui/data-table';
import DataTableActions from './data-table-actions.svelte';
import { tv, tvTransportType } from '$lib/i18n/vehicles';

export function createColumns(
    onSuccess: () => void,
    permissions: { canEdit: boolean; canDelete: boolean } = { canEdit: true, canDelete: true }
): ColumnDef<Vehicle>[] {
    const cols: ColumnDef<Vehicle>[] = [
        {
            accessorKey: 'vehicle_key',
            header: tv('col_key'),
            cell: ({ row }) => {
                return row.original.vehicle_key;
            }
        },
        {
            accessorKey: 'brand',
            header: tv('col_brand'),
            cell: ({ row }) => {
                return row.original.brand || '-';
            }
        },
        {
            accessorKey: 'year',
            header: tv('col_year'),
            cell: ({ row }) => {
                return row.original.year || '-';
            }
        },
        {
            accessorKey: 'plate_number',
            header: tv('col_plate'),
            cell: ({ row }) => {
                return row.original.plate_number || '-';
            }
        },
        {
            accessorKey: 'transporter_key',
            header: tv('col_transporter'),
            cell: ({ row }) => {
                return row.original.transporter_key || '-';
            }
        },
        {
            accessorKey: 'transport_type',
            header: tv('col_transport_type'),
            cell: ({ row }) => {
                if (!row.original.transport_type) return '-';
                return `${row.original.transport_type} - ${tvTransportType(row.original.transport_type, row.original.transport_type)}`;
            }
        }
    ];
    if (permissions.canEdit || permissions.canDelete) {
        cols.push({
            id: 'actions',
            header: tv('col_actions'),
            cell: ({ row }) => {
                return renderComponent(DataTableActions, {
                    item: row.original,
                    onSuccess,
                    canEdit: permissions.canEdit,
                    canDelete: permissions.canDelete
                });
            }
        });
    }
    return cols;
}
