<script lang="ts">
	import { browser } from "$app/environment";
	import { enhance } from "$app/forms";
	import Button from "$lib/components/Button.svelte";

	type User = {
		id: string;
		email?: string | null;
		firstName?: string | null;
		lastName?: string | null;
		appleId?: string | null;
	} | null;

	let {
		anonymousId,
		user,
		onSignIn,
	}: { anonymousId: string | null; user: User; onSignIn: () => void } = $props();

	let seeding = $state(false);

	async function seedHistory(days: number = 10) {
		if (!browser || !anonymousId || seeding) return;
		seeding = true;
		try {
			const response = await fetch("/api/dev/seed-history", {
				method: "POST",
				headers: { "Content-Type": "application/json" },
				body: JSON.stringify({ anonymousId, days })
			});
			const result = await response.json();
			alert(
				`Seeded! Inserted: ${result.inserted?.join(", ")}. Skipped (already exist): ${result.skipped?.join(", ") || "none"}`
			);
		} catch {
			alert("Failed to seed history");
		} finally {
			seeding = false;
		}
	}

	function clearLocalStorage() {
		if (!browser) return;
		// Clear all bibdle-related localStorage items
		const keysToRemove: string[] = [];
		for (let i = 0; i < localStorage.length; i++) {
			const key = localStorage.key(i);
			if (key && key.startsWith("bibdle-")) {
				keysToRemove.push(key);
			}
		}
		keysToRemove.forEach((key) => localStorage.removeItem(key));
		// Reload the page to reset state
		window.location.reload();
	}
</script>

<div class="pt-24 pb-4">
	<div class="border-t-2 border-gray-400"></div>
</div>
<div class="flex flex-col gap-3">
	<a
		href="/stats?{user
			? `userId=${user.id}`
			: `anonymousId=${anonymousId}`}&tz={encodeURIComponent(
			Intl.DateTimeFormat().resolvedOptions().timeZone,
		)}"
		class="inline-flex items-center justify-center w-full px-4 py-4 md:py-2 bg-amber-600 text-white rounded-lg hover:bg-amber-700 transition-colors text-sm font-medium shadow-md"
	>
		📊 View Stats
	</a>
	<a
		href="/progress"
		class="inline-flex items-center justify-center w-full px-4 py-4 md:py-2 bg-emerald-600 text-white rounded-lg hover:bg-emerald-700 transition-colors text-sm font-medium shadow-md"
	>
		📈 View Progress
	</a>

	{#if user}
		<form method="POST" action="/auth/logout" use:enhance class="w-full">
			<button
				type="submit"
				class="inline-flex items-center justify-center w-full px-4 py-4 md:py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors text-sm font-medium shadow-md"
			>
				🚪 Sign Out
			</button>
		</form>
	{:else}
		<button
			onclick={onSignIn}
			class="inline-flex items-center justify-center w-full px-4 py-4 md:py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors text-sm font-medium shadow-md"
		>
			🔐 Sign In
		</button>
	{/if}

	<div class="flex flex-col md:flex-row gap-3 md:gap-2">
		<Button
			variant="secondary"
			onclick={() => alert("About page coming soon!")}
			class="w-full md:w-auto py-4 md:py-2"
		>
			About Bibdle / FAQs
		</Button>
		<Button
			variant="google"
			onclick={() => alert("Google sign-in coming soon!")}
			class="w-full md:w-auto py-4 md:py-2"
		>
			<svg class="w-4 h-4 mr-2" viewBox="0 0 24 24">
				<path
					fill="#4285F4"
					d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"
				/>
				<path
					fill="#34A853"
					d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"
				/>
				<path
					fill="#FBBC05"
					d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"
				/>
				<path
					fill="#EA4335"
					d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"
				/>
			</svg>
			Sign in with Google
		</Button>
		<Button
			variant="apple"
			onclick={() => alert("Apple sign-in coming soon!")}
			class="w-full md:w-auto py-4 md:py-2"
		>
			<svg class="w-4 h-4 mr-2" viewBox="0 0 24 24" fill="currentColor">
				<path
					d="M17.05 20.28c-.98.95-2.05.88-3.08.4-1.09-.5-2.08-.48-3.24 0-1.44.62-2.2.44-3.06-.4C2.79 15.25 3.51 7.59 9.05 7.31c1.35.07 2.29.74 3.08.8 1.18-.24 2.31-.93 3.57-.84 1.51.12 2.65.72 3.4 1.8-3.12 1.87-2.38 5.98.48 7.13-.57 1.5-1.31 2.99-2.54 4.09l.01-.01zM12.03 7.25c-.15-2.23 1.66-4.07 3.74-4.25.29 2.58-2.34 4.5-3.74 4.25z"
				/>
			</svg>
			Sign in with Apple
		</Button>
	</div>

	<div class="flex flex-col md:flex-row gap-3 md:gap-2">
		<Button
			variant="primary"
			onclick={() => alert("Patreon coming soon!")}
			class="w-full md:w-auto py-4 md:py-2"
		>
			Become a Patron
		</Button>
	</div>

	<Button
		variant="danger"
		onclick={clearLocalStorage}
		class="w-full py-4 md:py-2"
	>
		Clear LocalStorage
	</Button>

	<Button
		variant="secondary"
		onclick={() => seedHistory(1)}
		disabled={seeding}
		class="w-full py-4 md:py-2"
	>
		{seeding ? "Seeding..." : "Add 1 Day of History"}
	</Button>
	<Button
		variant="secondary"
		onclick={() => seedHistory(10)}
		disabled={seeding}
		class="w-full py-4 md:py-2"
	>
		{seeding ? "Seeding..." : "Seed 10 Days of History"}
	</Button>
</div>
