<script lang="ts">
	import { fly, fade } from 'svelte/transition';
	import { getSnacks, removeSnack, type Snack } from '$lib/stores/snackbar.svelte';
	import MdiIcon from '$lib/components/MdiIcon.svelte';
	import { t } from '$lib/i18n';
	import { portal } from '$lib/portal';

	const snacks = $derived(getSnacks());

	let expandedIds = $state<Set<number>>(new Set());

	function toggleDetail(id: number) {
		const next = new Set(expandedIds);
		if (next.has(id)) next.delete(id);
		else next.add(id);
		expandedIds = next;
	}

	const iconMap: Record<string, string> = {
		success: 'mdiCheckCircle',
		error: 'mdiAlertCircle',
		info: 'mdiInformation',
		warning: 'mdiAlert',
	};

	const accentMap: Record<string, string> = {
		success: '#059669',
		error: '#ef4444',
		info: '#3b82f6',
		warning: '#f59e0b',
	};
</script>

{#if snacks.length > 0}
	<div use:portal class="snackbar-container">
		{#each snacks as snack (snack.id)}
			<div
				in:fly={{ y: 40, duration: 300 }}
				out:fade={{ duration: 200 }}
				class="snack-item"
				style="--snack-accent: {accentMap[snack.type]};"
			>
				<span class="snack-icon" style="color: {accentMap[snack.type]};">
					<MdiIcon name={iconMap[snack.type]} size={18} />
				</span>
				<div style="flex: 1; min-width: 0;">
					<p class="snack-message">{snack.message}</p>
					{#if snack.detail}
						<button class="snack-detail-toggle" onclick={() => toggleDetail(snack.id)}>
							{expandedIds.has(snack.id) ? t('snackbar.hideDetails') : t('snackbar.showDetails')}
						</button>
						{#if expandedIds.has(snack.id)}
							<pre class="snack-detail">{snack.detail}</pre>
						{/if}
					{/if}
				</div>
				<button class="snack-close" onclick={() => removeSnack(snack.id)} aria-label={t('common.dismiss')}>
					<MdiIcon name="mdiClose" size={14} />
				</button>
			</div>
		{/each}
	</div>
{/if}

<style>
	.snackbar-container {
		position: fixed;
		left: 50%;
		transform: translateX(-50%);
		z-index: 9999;
		display: flex;
		flex-direction: column;
		gap: 0.5rem;
		width: 90%;
		max-width: 26rem;
		pointer-events: none;
		bottom: 5rem;
	}
	@media (min-width: 768px) {
		.snackbar-container {
			bottom: 1.5rem;
		}
	}

	.snack-item {
		--snack-solid-bg: #131520;
		pointer-events: auto;
		display: flex;
		align-items: flex-start;
		gap: 0.625rem;
		padding: 0.85rem 1rem;
		border-radius: 14px;
		border-left: 3px solid var(--snack-accent);
		background: var(--snack-solid-bg);
		border-top: 1px solid var(--color-rule-strong);
		border-right: 1px solid var(--color-rule-strong);
		border-bottom: 1px solid var(--color-rule-strong);
		box-shadow: var(--shadow-card), 0 12px 30px -10px rgba(0, 0, 0, 0.4);
	}
	:global([data-theme="light"]) .snack-item { --snack-solid-bg: #fafafe; }

	:global([data-theme="dark"]) .snack-item {
		box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4), 0 0 16px color-mix(in srgb, var(--snack-accent) 10%, transparent);
	}

	.snack-icon {
		flex-shrink: 0;
		margin-top: 1px;
	}

	.snack-message {
		font-size: 0.8rem;
		line-height: 1.4;
		margin: 0;
		color: var(--color-foreground);
	}

	.snack-detail-toggle {
		font-size: 0.7rem;
		color: var(--color-muted-foreground);
		background: none;
		border: none;
		padding: 0;
		margin-top: 0.25rem;
		cursor: pointer;
		text-decoration: underline;
		text-underline-offset: 2px;
	}

	.snack-detail-toggle:hover {
		color: var(--color-foreground);
	}

	.snack-detail {
		font-size: 0.7rem;
		font-family: var(--font-mono);
		color: var(--color-muted-foreground);
		margin: 0.25rem 0 0;
		white-space: pre-wrap;
		word-break: break-word;
	}

	.snack-close {
		flex-shrink: 0;
		background: none;
		border: none;
		color: var(--color-muted-foreground);
		cursor: pointer;
		padding: 0.125rem;
		border-radius: 0.25rem;
		transition: all 0.15s ease;
		line-height: 1;
	}

	.snack-close:hover {
		color: var(--color-foreground);
		background: var(--color-muted);
	}
</style>
