<script lang="ts">
	import { onMount } from 'svelte';
	import { simulation } from '$lib/stores/simulation.svelte';
	import { initWasm } from '$lib/wasm/loader';
	import { getBodyInfos } from '$lib/wasm/bridge';
	import Viewport from './(simulator)/components/Viewport.svelte';
	import TimeControls from './(simulator)/components/TimeControls.svelte';
	import ViewToggle from './(simulator)/components/ViewToggle.svelte';
	import StationPanel from './(simulator)/components/StationPanel.svelte';
	import RoutingPanel from './(simulator)/components/RoutingPanel.svelte';

	let wasmError = $state('');

	onMount(async () => {
		try {
			await initWasm();
			simulation.bodyInfos = getBodyInfos();
			simulation.wasmReady = true;
			simulation.setDate(2025, 1, 1);
			simulation.isPlaying = true;
		} catch (e) {
			wasmError = String(e);
			console.error('Failed to load WASM module:', e);
		}
	});
</script>

<div class="h-screen w-screen flex flex-col bg-[var(--bg-primary)]">
	<!-- Top bar -->
	<header class="flex items-center justify-between px-4 py-2 bg-[var(--bg-secondary)] border-b border-white/5 shrink-0">
		<div class="flex items-center gap-3">
			<h1 class="text-sm font-bold tracking-wide text-[var(--text-primary)]">
				MASS DRIVER
			</h1>
			<span class="text-xs text-[var(--text-secondary)]">Interplanetary Relay Network</span>
		</div>
		<ViewToggle />
	</header>

	<!-- Main content -->
	<main class="flex-1 flex overflow-hidden">
		{#if wasmError}
			<div class="flex-1 flex items-center justify-center">
				<div class="bg-red-900/50 border border-red-500/30 rounded-lg p-6 max-w-md">
					<h2 class="text-red-400 font-bold mb-2">Failed to load simulation engine</h2>
					<p class="text-sm text-red-300/80">{wasmError}</p>
				</div>
			</div>
		{:else if !simulation.wasmReady}
			<div class="flex-1 flex items-center justify-center">
				<div class="text-center">
					<div class="w-8 h-8 border-2 border-[var(--accent-blue)] border-t-transparent rounded-full animate-spin mx-auto mb-3"></div>
					<p class="text-sm text-[var(--text-secondary)]">Initializing simulation engine...</p>
				</div>
			</div>
		{:else}
			<!-- Viewport -->
			<div class="flex-1 relative">
				<Viewport />
			</div>
			<!-- Right sidebar -->
			<aside class="w-64 shrink-0 p-2 bg-[var(--bg-secondary)] border-l border-white/5 overflow-y-auto flex flex-col gap-2">
				<StationPanel />
				<RoutingPanel />
			</aside>
		{/if}
	</main>

	<!-- Bottom bar: time controls -->
	<footer class="px-4 py-2 bg-[var(--bg-secondary)] border-t border-white/5 shrink-0">
		<TimeControls />
	</footer>
</div>
