import type { DodaAltaLog } from '$lib/api/dashboard/a76/doda-alta-log';
import type { ColumnDef } from '@tanstack/table-core';
import { createRawSnippet } from 'svelte';
import { renderSnippet } from '$lib/components/ui/data-table';

function formatDateTime(raw?: string | null): string {
	if (!raw) return '-';
	try {
		return new Date(raw).toLocaleString('es-MX', {
			day: '2-digit',
			month: '2-digit',
			year: 'numeric',
			hour: '2-digit',
			minute: '2-digit'
		});
	} catch {
		return raw;
	}
}

const STATUS_CLASSES: Record<string, string> = {
	success: 'bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-400',
	failed: 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400',
	failure: 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-400',
	pending: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-400',
	processing: 'bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-400',
	started: 'bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-400'
};

function statusBadge(status: string | null | undefined) {
	const s = (status || '').toLowerCase();
	const cls = STATUS_CLASSES[s] || 'bg-gray-100 text-gray-700';
	return createRawSnippet(() => ({
		render: () =>
			`<span class="inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium ${cls}">${status || '-'}</span>`
	}));
}

export function createAltaLogColumns(): ColumnDef<DodaAltaLog>[] {
	return [
		{
			accessorKey: 'id',
			header: '#',
			size: 60,
			cell: ({ row }) => row.original.id
		},
		{
			accessorKey: 'doda_id',
			header: 'DODA ID',
			size: 80,
			cell: ({ row }) => row.original.doda_id ?? '-'
		},
		{
			accessorKey: 'integration_number',
			header: 'No. Integración',
			cell: ({ row }) => row.original.integration_number || '-'
		},
		{
			accessorKey: 'variant',
			header: 'Tipo',
			size: 70,
			cell: ({ row }) => (row.original.variant || '-').toUpperCase()
		},
		{
			accessorKey: 'patent',
			header: 'Patente',
			size: 80,
			cell: ({ row }) => row.original.patent || '-'
		},
		{
			accessorKey: 'dispatch_customs',
			header: 'Aduana',
			size: 80,
			cell: ({ row }) => row.original.dispatch_customs || '-'
		},
		{
			accessorKey: 'operation_type',
			header: 'Operación',
			size: 90,
			cell: ({ row }) => row.original.operation_type || '-'
		},
		{
			accessorKey: 'status',
			header: 'Estatus',
			size: 110,
			cell: ({ row }) => renderSnippet(statusBadge(row.original.status), {})
		},
		{
			accessorKey: 'task_id',
			header: 'Task ID',
			cell: ({ row }) => {
				const id = row.original.task_id || '';
				const snippet = createRawSnippet(() => ({
					render: () =>
						`<span class="font-mono text-xs truncate max-w-[180px] block" title="${id}">${id || '-'}</span>`
				}));
				return renderSnippet(snippet, {});
			}
		},
		{
			accessorKey: 'message',
			header: 'Mensaje',
			cell: ({ row }) => {
				const msg = row.original.message || '';
				const snippet = createRawSnippet(() => ({
					render: () =>
						`<span class="text-xs truncate max-w-[220px] block text-muted-foreground" title="${msg}">${msg || '-'}</span>`
				}));
				return renderSnippet(snippet, {});
			}
		},
		{
			accessorKey: 'created_at',
			header: 'Fecha',
			size: 130,
			cell: ({ row }) => formatDateTime(row.original.created_at)
		}
	];
}
