<script lang="ts">
	import { Button } from '$lib/components/ui/button';
	import * as AlertDialog from '$lib/components/ui/alert-dialog';
	import { portsApi, type Port } from '$lib/api/dashboard/a76/general_catalogs/ports';
	import { LoaderCircle } from 'lucide-svelte';
	import { companyStore } from '$lib/stores/company.svelte';

	let {
		open = $bindable(false),
		item,
		onSuccess
	}: {
		open: boolean;
		item: Port | null;
		onSuccess?: () => void;
	} = $props();

	let loading = $state(false);
	let error = $state<string | null>(null);

	async function handleDelete() {
		if (!item) return;

		const companyId = companyStore.activeCompany?.id;
		if (!companyId) {
			error = 'Selecciona una compañía antes de eliminar';
			return;
		}

		loading = true;
		error = null;

		try {
			const response = await portsApi.delete(item.id, companyId);

			if (response.error) {
				error = response.error;
				return;
			}

			// Éxito
			open = false;
			if (onSuccess) {
				onSuccess();
			}
		} catch (e) {
			error = e instanceof Error ? e.message : 'Error al eliminar';
			console.error('Error deleting:', e);
		} finally {
			loading = false;
		}
	}

	function handleOpenChange(newOpen: boolean) {
		if (!newOpen) {
			error = null;
		}
		open = newOpen;
	}
</script>

<AlertDialog.Root bind:open onOpenChange={handleOpenChange}>
	<AlertDialog.Content>
		<AlertDialog.Header>
			<AlertDialog.Title>¿Estás seguro?</AlertDialog.Title>
			<AlertDialog.Description class="space-y-2">
				<p>Esta acción no se puede deshacer. Se eliminará permanentemente este puerto:</p>
				{#if item}
					<div class="mt-2 rounded-lg bg-muted p-3 space-y-2">
						<div class="flex items-center justify-between text-sm">
							<span class="font-medium">Código:</span>
							<code class="font-mono font-semibold">{item.port_code}</code>
						</div>
						{#if item.description}
							<div class="flex flex-col gap-1 text-sm">
								<span class="font-medium">Descripción:</span>
								<span class="text-xs">{item.description}</span>
							</div>
						{/if}
						{#if item.location_code}
							<div class="flex items-center justify-between text-sm">
								<span class="font-medium">Ubicación:</span>
								<code class="font-mono font-semibold">{item.location_code}</code>
							</div>
						{/if}
					</div>
				{:else}
					<div class="mt-2 rounded-lg bg-muted p-3 text-sm italic">
						Cargando información del puerto...
					</div>
				{/if}
				{#if error}
					<div
						class="mt-2 rounded-lg border border-destructive bg-destructive/10 p-3 text-sm text-destructive"
					>
						{error}
					</div>
				{/if}
			</AlertDialog.Description>
		</AlertDialog.Header>
		<AlertDialog.Footer>
			<AlertDialog.Cancel disabled={loading}>Cancelar</AlertDialog.Cancel>
			<AlertDialog.Action
				onclick={handleDelete}
				disabled={loading}
				class="bg-destructive text-destructive-foreground hover:bg-destructive/90"
			>
				{#if loading}
					<LoaderCircle class="mr-2 h-4 w-4 animate-spin" />
					Eliminando...
				{:else}
					Eliminar
				{/if}
			</AlertDialog.Action>
		</AlertDialog.Footer>
	</AlertDialog.Content>
</AlertDialog.Root>
