<script lang="ts">
	import { onMount, tick } from 'svelte';
	import {
		AuditLogAPI,
		type AuditLog,
		type AuditLogParams
	} from '$lib/api/dashboard/a76/audit_log';
	import * as Card from '$lib/components/ui/card';
	import { Button } from '$lib/components/ui/button';
	import { Input } from '$lib/components/ui/input';
	import { Label } from '$lib/components/ui/label';
	import * as Table from '$lib/components/ui/table';
	import { RefreshCw, Search } from 'lucide-svelte';

	let logs: AuditLog[] = [];
	let total = 0;
	let page = 1;
	const pageSize = 50;
	let loading = false;
	let error: string | null = null;

	let search = '';
	let usernameFilter = '';
	let procedureFilter = '';
	let dateFrom = '';
	let dateTo = '';

	let procedures: string[] = [];

	let hasMore = true;
	let sentinel: HTMLElement;

	async function loadLogs() {
		if (loading) return;
		loading = true;
		error = null;
		try {
			const params: AuditLogParams = {
				page,
				page_size: pageSize,
				search: search || undefined,
				username: usernameFilter || undefined,
				procedure: procedureFilter || undefined,
				date_from: dateFrom || undefined,
				date_to: dateTo || undefined
			};

			const response = await AuditLogAPI.getLogs(params);

			if (page === 1) {
				logs = response.data;
			} else {
				logs = [...logs, ...response.data];
			}

			total = response.total;
			hasMore = logs.length < total;
		} catch (e: unknown) {
			error = e instanceof Error ? e.message : String(e);
		} finally {
			loading = false;
		}
	}

	async function loadProcedures() {
		try {
			procedures = await AuditLogAPI.getProcedures();
		} catch (e) {
			console.error(e);
		}
	}

	let timer: ReturnType<typeof setTimeout>;

	function handleFilterChange() {
		page = 1;
		hasMore = true;
		logs = [];
		loadLogs();
	}

	function handleSearchInput() {
		clearTimeout(timer);
		timer = setTimeout(() => {
			page = 1;
			hasMore = true;
			logs = [];
			loadLogs();
		}, 300);
	}

	function clearFilters() {
		search = '';
		usernameFilter = '';
		procedureFilter = '';
		dateFrom = '';
		dateTo = '';
		handleFilterChange();
	}

	function formatDate(dateStr: string, timestamp?: string): string {
		if (timestamp) {
			return new Date(timestamp).toLocaleDateString();
		}
		if (!dateStr) return '';
		const [y, m, d] = dateStr.split('-');
		return `${d}/${m}/${y}`;
	}

	function formatTime(timeStr: string, timestamp?: string): string {
		if (timestamp) {
			return new Date(timestamp).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
		}
		if (!timeStr) return '';
		try {
			const [h, m] = timeStr.split(':');
			let hour = parseInt(h, 10);
			const ampm = hour >= 12 ? 'PM' : 'AM';
			hour = hour % 12;
			hour = hour ? hour : 12;
			return `${hour.toString().padStart(2, '0')}:${m} ${ampm}`;
		} catch {
			return timeStr;
		}
	}

	onMount(() => {
		void loadProcedures();
		void loadLogs();

		const observer = new IntersectionObserver(
			(entries) => {
				if (entries[0]?.isIntersecting && hasMore && !loading) {
					page++;
					void loadLogs();
				}
			},
			{ rootMargin: '100px' }
		);

		let cancelled = false;
		void tick().then(() => {
			if (!cancelled && sentinel) {
				observer.observe(sentinel);
			}
		});

		return () => {
			cancelled = true;
			observer.disconnect();
		};
	});
</script>

<div class="flex min-h-0 flex-1 flex-col gap-4">
	<div class="flex flex-none items-center justify-end">
		<Button
			variant="outline"
			onclick={() => {
				page = 1;
				hasMore = true;
				logs = [];
				void loadLogs();
			}}
			disabled={loading}
		>
			<RefreshCw class="mr-2 h-4 w-4 {loading ? 'animate-spin' : ''}" />
			Actualizar
		</Button>
	</div>

	<div class="flex-none">
		<Card.Root>
			<Card.Header class="py-3">
				<Card.Title class="text-lg">Filtros</Card.Title>
			</Card.Header>
			<Card.Content>
				<div class="grid grid-cols-1 gap-4 md:grid-cols-3 lg:grid-cols-5">
					<div class="space-y-1">
						<Label for="search" class="text-xs">Búsqueda General</Label>
						<div class="relative">
							<Search class="absolute top-2.5 left-2 h-4 w-4 text-muted-foreground" />
							<Input
								id="search"
								placeholder="Ref, Mov, Usuario..."
								class="h-9 pl-8"
								bind:value={search}
								oninput={handleSearchInput}
							/>
						</div>
					</div>

					<div class="space-y-1">
						<Label for="username" class="text-xs">Usuario</Label>
						<Input
							id="username"
							placeholder="Filtrar por usuario"
							class="h-9"
							bind:value={usernameFilter}
							oninput={handleSearchInput}
						/>
					</div>

					<div class="space-y-1">
						<Label for="procedure" class="text-xs">Procedimiento</Label>
						<select
							id="procedure"
							bind:value={procedureFilter}
							onchange={handleFilterChange}
							class="flex h-9 w-full rounded-md border border-input bg-background px-3 py-1 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-50"
						>
							<option value="">Todos</option>
							{#each procedures as proc}
								<option value={proc}>{proc}</option>
							{/each}
						</select>
					</div>

					<div class="space-y-1">
						<Label for="dateFrom" class="text-xs">Desde</Label>
						<Input
							id="dateFrom"
							type="date"
							class="h-9"
							bind:value={dateFrom}
							onchange={handleFilterChange}
						/>
					</div>

					<div class="space-y-1">
						<Label for="dateTo" class="text-xs">Hasta</Label>
						<Input
							id="dateTo"
							type="date"
							class="h-9"
							bind:value={dateTo}
							onchange={handleFilterChange}
						/>
					</div>
				</div>
				<div class="mt-2 flex justify-end">
					<Button variant="ghost" onclick={clearFilters} size="sm" class="h-8 text-xs font-normal">
						Limpiar Filtros
					</Button>
				</div>
			</Card.Content>
		</Card.Root>
	</div>

	<Card.Root class="flex min-h-0 flex-1 flex-col overflow-hidden">
		<Card.Header class="flex-none py-3">
			<div class="flex items-center justify-between">
				<div>
					<Card.Title class="text-lg">Registros</Card.Title>
					<Card.Description class="text-xs">Total: {total} registros encontrados</Card.Description>
				</div>
			</div>
		</Card.Header>
		<Card.Content class="flex min-h-0 flex-1 flex-col p-0 px-6 pb-6">
			{#if error}
				<div class="rounded-md bg-red-50 p-4 text-center text-red-500 dark:bg-red-950/30 dark:text-red-400">
					Error al cargar datos: {error}
				</div>
			{:else}
				<div class="relative min-h-0 flex-1 overflow-y-auto rounded-md border bg-card shadow-inner">
					<Table.Root>
						<Table.Header class="sticky top-0 z-10 bg-background/95 shadow-sm backdrop-blur-sm">
							<Table.Row>
								<Table.Head class="w-[80px]">ID</Table.Head>
								<Table.Head class="w-[180px]">Referencia</Table.Head>
								<Table.Head>Procedimiento</Table.Head>
								<Table.Head>Movimiento</Table.Head>
								<Table.Head class="w-[150px]">Usuario</Table.Head>
								<Table.Head class="w-[120px]">Fecha</Table.Head>
								<Table.Head class="w-[120px]">Hora</Table.Head>
							</Table.Row>
						</Table.Header>
						<Table.Body>
							{#if loading && logs.length === 0}
								<Table.Row>
									<Table.Cell colspan={7} class="h-24 text-center text-muted-foreground italic">
										Cargando...
									</Table.Cell>
								</Table.Row>
							{:else if logs.length === 0}
								<Table.Row>
									<Table.Cell colspan={7} class="h-24 text-center text-muted-foreground">
										No se encontraron registros
									</Table.Cell>
								</Table.Row>
							{:else}
								{#each logs as log}
									<Table.Row class="transition-colors hover:bg-muted/50">
										<Table.Cell class="text-xs font-medium text-muted-foreground">{log.spec_id}</Table.Cell>
										<Table.Cell class="text-sm font-bold text-blue-600 dark:text-blue-400">
											{log.reference}
										</Table.Cell>
										<Table.Cell class="text-sm">{log.procedure}</Table.Cell>
										<Table.Cell class="text-sm">{log.movement}</Table.Cell>
										<Table.Cell class="text-sm">{log.username}</Table.Cell>
										<Table.Cell class="text-sm">{formatDate(log.date, log.timestamp)}</Table.Cell>
										<Table.Cell class="text-sm">{formatTime(log.time, log.timestamp)}</Table.Cell>
									</Table.Row>
								{/each}
							{/if}
						</Table.Body>
					</Table.Root>

					<div bind:this={sentinel} class="flex h-12 w-full items-center justify-center p-4">
						{#if loading && logs.length > 0}
							<div class="flex items-center gap-2">
								<RefreshCw class="h-4 w-4 animate-spin text-primary" />
								<span class="text-sm text-muted-foreground">Cargando más registros...</span>
							</div>
						{/if}
					</div>
				</div>
			{/if}
		</Card.Content>
	</Card.Root>
</div>
