<script lang="ts">
	import Map from '$lib/components/Map.svelte';
	import StatTile from '$lib/components/StatTile.svelte';
	import ActivityItem from '$lib/components/ActivityItem.svelte';
	import { fmtDistance, fmtDuration, fmtElevation } from '$lib/format';

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

<svelte:head>
	<title>Dashboard · Streba</title>
</svelte:head>

<h1>Dashboard</h1>
<p class="page-sub">Every track you've recorded, in one place.</p>

{#if data.totals.count === 0}
	<div class="card empty">
		<p><strong>Welcome to Streba.</strong></p>
		<p>Upload your first GPX track to get going, or start ticking off Alpine peaks right away.</p>
		<div class="empty-actions">
			<a class="btn" href="/upload">Upload a GPX file</a>
			<a class="btn ghost" href="/peaks">Browse the peaks</a>
		</div>
	</div>
{:else}
	<div class="kpis">
		<StatTile label="Activities" value={String(data.totals.count)} />
		<StatTile label="Total distance" value={fmtDistance(data.totals.distance_m)} />
		<StatTile label="Total ascent" value={fmtElevation(data.totals.elev_gain_m)} />
		<StatTile label="Moving time" value={fmtDuration(data.totals.moving_s)} />
	</div>

	<h2>Worldmap</h2>
	<Map tracks={data.tracks} height="440px" />

	<div class="lower">
		<section>
			<h2>Recent activities</h2>
			<div class="card list">
				{#each data.activities.slice(0, 6) as activity (activity.id)}
					<ActivityItem {activity} />
				{/each}
			</div>
		</section>
		<section>
			<h2>Peak bagging</h2>
			<div class="card peak-progress">
				<div class="meter-nums">
					<span class="big">{data.peaksClimbed}</span>
					<span class="of">of {data.peaksTotal} peaks climbed</span>
				</div>
				<div class="meter" role="meter" aria-valuemin="0" aria-valuemax={data.peaksTotal} aria-valuenow={data.peaksClimbed} aria-label="Peaks climbed">
					<div class="fill" style:width="{(data.peaksClimbed / data.peaksTotal) * 100}%"></div>
				</div>
				<a class="btn ghost" href="/peaks">Open the checklist</a>
			</div>
		</section>
	</div>
{/if}

<style>
	.kpis {
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
		gap: 0.75rem;
		margin-bottom: 0.5rem;
	}
	.empty {
		padding: 2.5rem;
		text-align: center;
	}
	.empty p {
		margin: 0.25rem 0;
		color: var(--text-secondary);
	}
	.empty p strong {
		color: var(--text-primary);
		font-size: 1.1rem;
	}
	.empty-actions {
		display: flex;
		gap: 0.75rem;
		justify-content: center;
		margin-top: 1.25rem;
	}
	.lower {
		display: grid;
		grid-template-columns: 3fr 2fr;
		gap: 1.25rem;
		align-items: start;
	}
	@media (max-width: 720px) {
		.lower {
			grid-template-columns: 1fr;
		}
	}
	.list :global(.item:not(:last-child)) {
		border-bottom: 1px solid var(--border);
	}
	.peak-progress {
		padding: 1.15rem;
		display: flex;
		flex-direction: column;
		gap: 0.85rem;
		align-items: flex-start;
	}
	.meter-nums .big {
		font-size: 2rem;
		font-weight: 700;
		letter-spacing: -0.02em;
	}
	.meter-nums .of {
		color: var(--text-secondary);
		font-size: 0.9rem;
		margin-left: 0.4rem;
	}
	.meter {
		width: 100%;
		height: 8px;
		border-radius: 999px;
		background: var(--accent-track);
	}
	.fill {
		height: 100%;
		border-radius: 999px;
		background: var(--accent);
		min-width: 2px;
	}
</style>
