<script lang="ts">
	import { onMount } from 'svelte';
	import { goto } from '$app/navigation';
	import { page } from '$app/stores';
	import { getSupabase } from '$lib/supabase';
	import * as m from '$lib/paraglide/messages';

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

	onMount(async () => {
		const code = $page.url.searchParams.get('code');

		if (!code) {
			error = 'No authorization code received.';
			return;
		}

		const { error: exchangeError } = await getSupabase().auth.exchangeCodeForSession(code);

		if (exchangeError) {
			error = exchangeError.message;
			return;
		}

		// onAuthStateChange in root layout handles post-login routing
		goto('/');
	});
</script>

<div class="flex h-screen items-center justify-center bg-background">
	{#if error}
		<div class="max-w-sm text-center">
			<p class="mb-4 text-sm text-red-600">{error}</p>
			<a href="/" class="text-sm text-slate-600 underline">{m.auth_back_to_home()}</a>
		</div>
	{:else}
		<p class="text-sm text-text-secondary">{m.loading()}</p>
	{/if}
</div>
