<script lang="ts">
	import { fade, scale } from 'svelte/transition';
	import type { AddressScope } from '$lib/graphql/queries/account';

	interface Props {
		open: boolean;
		title: string;
		scopes: AddressScope[];
		onClose: () => void;
	}

	let { open, title, scopes, onClose }: Props = $props();

	function handleKeydown(event: KeyboardEvent) {
		if (event.key === 'Escape') {
			onClose();
		}
	}

	// Lock body scroll when open
	$effect(() => {
		if (open) {
			document.body.style.overflow = 'hidden';
			return () => {
				document.body.style.overflow = '';
			};
		}
	});
</script>

<svelte:window onkeydown={handleKeydown} />

{#if open}
	<!-- Backdrop -->
	<button
		type="button"
		class="fixed inset-0 z-50 cursor-default bg-overlay backdrop-blur-sm"
		onclick={onClose}
		aria-label="Close modal"
		transition:fade={{ duration: 200 }}
	></button>

	<!-- Modal -->
	<div
		class="pointer-events-none fixed inset-0 z-50 flex items-center justify-center"
		role="dialog"
		aria-modal="true"
		aria-labelledby="scope-list-modal-title"
		tabindex="-1"
		transition:fade={{ duration: 200 }}
	>
		<div
			class="pointer-events-auto mx-4 w-full max-w-lg rounded-xl border border-primary-500/20 bg-theme-card shadow-2xl"
			transition:scale={{ start: 0.95, duration: 200 }}
		>
			<!-- Header -->
			<div
				class="border-b border-primary-500/20 bg-linear-to-r from-primary-500/10 to-transparent px-5 py-4"
			>
				<h2 id="scope-list-modal-title" class="text-lg font-semibold text-theme">
					{title}
				</h2>
			</div>

			<!-- Body -->
			<div class="max-h-[60vh] overflow-y-auto px-5 py-5">
				{#if scopes.length > 0}
					<div class="space-y-4">
						{#each scopes as scope (scope.id)}
							<div class="bg-theme-secondary/30 rounded-lg border border-theme p-4">
								<h3 class="mb-2 font-medium text-theme">{scope.name || 'Service Scope'}</h3>
								{#if scope.description}
									<p class="mb-3 text-sm text-theme-secondary">{scope.description}</p>
								{/if}
								<div class="space-y-1">
									<p class="text-xs text-theme-muted">
										{scope.areas.length} area{scope.areas.length === 1 ? '' : 's'}
									</p>
									{#if scope.areas.length > 0}
										<div class="flex flex-wrap gap-1">
											{#each scope.areas.slice(0, 5) as area}
												<span
													class="rounded bg-theme-secondary px-2 py-0.5 text-xs text-theme-muted"
												>
													{area.name}
												</span>
											{/each}
											{#if scope.areas.length > 5}
												<span class="text-xs text-theme-muted">+{scope.areas.length - 5} more</span>
											{/if}
										</div>
									{/if}
								</div>
							</div>
						{/each}
					</div>
				{:else}
					<p class="py-4 text-center text-theme-muted">No previous scopes.</p>
				{/if}
			</div>

			<!-- Footer -->
			<div class="flex justify-end border-t border-theme px-5 py-4">
				<button type="button" class="btn-primary" onclick={onClose}> Close </button>
			</div>
		</div>
	</div>
{/if}
