import type { ColumnDef } from '@tanstack/table-core';
import type { ExchangeRate } from '$lib/api/dashboard/a76/general_catalogs/exchange-rate';
import { renderComponent } from '$lib/components/ui/data-table';
import DataTableActions from './data-table-actions.svelte';

export function createColumns(
	onSuccess?: () => void,
	{ canEdit = true, canDelete = true }: { canEdit?: boolean; canDelete?: boolean } = {}
): ColumnDef<ExchangeRate>[] {
	return [
		{
			accessorKey: 'date',
			header: 'Fecha',
			cell: ({ row }) => {
				const dateStr = row.original.date;
				if (!dateStr) return 'N/A';
				const date = new Date(dateStr);
				return date.toLocaleDateString('es-MX');
			}
		},
		{
			accessorKey: 'value',
			header: 'Valor',
			cell: ({ row }) => {
				const value = row.original.value;
				if (value === null || value === undefined) return 'N/A';
				return Number(value).toFixed(6);
			}
		},
		{
			accessorKey: 'local_currency',
			header: 'Moneda Local',
			cell: ({ row }) => row.original.local_currency ?? 'N/A'
		},
		{
			accessorKey: 'foreign_currency',
			header: 'Moneda Extranjera',
			cell: ({ row }) => row.original.foreign_currency ?? 'N/A'
		},
		{
			id: 'actions',
			header: 'Acciones',
			cell: ({ row }) => {
				return renderComponent(DataTableActions, {
					item: row.original,
					onSuccess
				});
			}
		}
	];
}