<script lang="ts">
	import { page } from '$app/stores';
	import Card from '$lib/components/Card.svelte';

	export let data;

	$: user = data.user;
</script>

<section class="w-full flex-col space-y-4">
	<header>
		<h1 class="text-xl font-semibold">Tableau de bord</h1>
		<p class="text-highlight-secondary">Ceci est la page d&apos;accueil du dashboard</p>
	</header>
	<main class="flex-col space-y-4">
		<div
			class="w-full flex-col justify-between space-x-0 space-y-4 md:flex md:flex-row md:space-x-6 md:space-y-0"
		>
			<Card title="Puzzles résolus" data={user?.completions ?? 0} />
			<Card title="Badges obtenus" data={user?.badges?.length ?? 'Aucun'} />
			<Card title="Rang actuel" data={user?.rank ?? 'Non classé'} />
		</div>
		<div class="grid grid-cols-1 gap-4">
			<div class="flex flex-col space-y-4">
				<header>
					<h2 class="text-lg font-semibold">Derniers puzzles</h2>
					<p class="text-highlight-secondary">
						Voici les derniers puzzles que vous avez résolus ou essayer de résoudres
					</p>
				</header>
				<div
					class="h-full max-h-96 overflow-y-scroll rounded-lg border-2 border-highlight-primary bg-primary-700 p-4 shadow-md"
				>
					<ul class="flex flex-col space-y-2">
						{#if user?.completionsList && user.completionsList.length > 0}
							{#each user.completionsList as completion, key}
								<li class="flex justify-between space-x-2">
									<div class="flex items-center space-x-4">
										<div class="flex items-center space-x-2">
											<span class="text-lg">{completion.puzzleName}</span>
										</div>
									</div>
									<div class="flex items-center space-x-4">
										<div class="flex flex-col">
											<span class="text-sm font-semibold">
												Essai{completion.tries > 1 ? 's' : ''}
											</span>
											<span class="text-right text-lg text-highlight-secondary">
												{completion.tries}
											</span>
										</div>
										<div class="flex flex-col">
											<span class="text-sm font-semibold">Score</span>
											<span class="text-right text-lg text-highlight-secondary">
												{completion.score}
											</span>
										</div>
									</div>
								</li>
							{/each}
						{:else}
							<li class="m-auto flex items-center justify-center">
								<span class="text-lg text-highlight-secondary"> Aucun puzzles </span>
							</li>
						{/if}
					</ul>
				</div>
			</div>
		</div>
	</main>
</section>
