<script lang="ts">
	import type { PageProps } from './$types';

	import { Button } from '$lib/components/ui/button';
	import * as Card from '$lib/components/ui/card';

	let { data }: PageProps = $props();
</script>

<section class="flex w-full flex-col gap-4">
	<header>
		<h1 class="text-xl font-semibold">Tableau de bord</h1>
		<p class="text-muted-foreground">Ceci est la page d&apos;accueil du dashboard</p>
	</header>
	<main class="flex flex-col gap-4">
		<div
			class="flex w-full flex-col justify-between gap-4 space-x-0 md:flex md:flex-row md:space-x-6 md:space-y-0"
		>
			{#snippet card({ title, data }: { title: string; data: string | number })}
				<Card.Root class="w-full rounded">
					<Card.Header class="p-4">
						<Card.Title>{data}</Card.Title>
						<Card.Description>{title}</Card.Description>
					</Card.Header>
				</Card.Root>
			{/snippet}
			{@render card({ title: 'Puzzles résolus', data: data.user?.completions ?? 0 })}
			{@render card({ title: 'Badges obtenus', data: data.user?.badges?.length ?? 'Aucun' })}
			{@render card({ title: 'Rang actuel', data: data.user?.rank ?? 'Non classé' })}
		</div>
		{#if data.event}
			<header>
				<h1 class="text-lg font-semibold">Événement en cours ou à venir</h1>
				<p class="text-muted-foreground">
					Partipiez à l&apos;événement en équipe en résolvant des puzzles
				</p>
			</header>
			<div
				class="flex items-center justify-between gap-4 rounded border border-primary bg-card px-4 py-2"
			>
				<div class="flex w-full flex-col justify-between md:flex-row md:items-center md:gap-4">
					<span class="text-lg font-semibold">{data.event.name}</span>
					<span class="text-muted-foreground"> Participer en équipe de 3 à 6 joueurs </span>
				</div>
				<Button href="/chapters/{data.event.id}/groups">Participer</Button>
			</div>
		{/if}
		<div class="grid grid-cols-1 gap-4">
			<div class="flex flex-col gap-4">
				<header>
					<h2 class="text-lg font-semibold">Derniers puzzles</h2>
					<p class="text-muted-foreground">
						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 border border-border bg-card p-4 shadow-md"
				>
					<ul class="flex flex-col space-y-2">
						{#if data.user?.completionsList?.length}
							{#each data.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 font-semibold">{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-muted-foreground">
												{completion.tries}
											</span>
										</div>
										<div class="flex flex-col">
											<span class="text-sm font-semibold">Score</span>
											<span class="text-right text-lg text-muted-foreground">
												{completion.score}
											</span>
										</div>
									</div>
								</li>
							{/each}
						{:else}
							<li class="m-auto flex items-center justify-center">
								<span class="text-lg text-muted-foreground"> Aucun puzzles </span>
							</li>
						{/if}
					</ul>
				</div>
			</div>
		</div>
	</main>
</section>
