<script lang="ts">
	import logo from '$lib/assets/round-logo-text.svg';
	import type { PageData } from './$types';
	import { resolve } from '$app/paths';

	let { data }: { data: PageData } = $props();
	let searchTerm = $state('');

	let filteredMaps = $derived(
		data.maps.filter((m) => {
			const term = searchTerm.toLowerCase();

			if (m.routeId.toString().includes(term)) return true;

			return m.contents.some((location) => location.toLowerCase().includes(term));
		})
	);

	const vibrate = (pattern: number | number[] = 10) => {
		if (typeof window !== 'undefined' && window.navigator.vibrate) {
			window.navigator.vibrate(pattern);
		}
	};

	const isVerifiedRecently = (dateVal: string | null) => {
		if (!dateVal) return 'draft';

		const checkedDate = new Date(dateVal).getTime();
		const oneYearAgo = Date.now() - 365 * 24 * 60 * 60 * 1000;

		return checkedDate > oneYearAgo ? 'verified' : 'stale';
	};

	const formatDate = (dateVal: string | null) => {
		if (!dateVal) return 'N/A';

		const date = new Date(dateVal);

		// Check for invalid dates to avoid "NaN/NaN/NaN"
		if (isNaN(date.getTime())) return 'Invalid Date';

		const d = date.getDate().toString().padStart(2, '0');
		const m = (date.getMonth() + 1).toString().padStart(2, '0');
		const y = date.getFullYear().toString().slice(-2);

		return `${d}/${m}/${y}`;
	};
</script>

<div class="page-wrapper">
	<header class="main-header">
		<div class="brand-container">
			<img src={logo} alt="OwlBoard Logo" class="main-logo" />
		</div>
	</header>

	<div class="list-container">
		<a href="https://owlboard.info" class="button-link">Go to OwlBoard Live Departures & PIS</a>

		<input type="text" bind:value={searchTerm} placeholder="Search Station/Jn" class="search-input" />
		{#each filteredMaps as map (map.routeId)}
			<a
				href={resolve(`/map/${map.routeId.toString().padStart(4, '0')}`)}
				class="card"
				onclick={() => vibrate(10)}
			>
				<div class="card-top">
					<span class="route-id">{map.routeId.toString().padStart(4, '0')}</span>
					<span class="status-badge {isVerifiedRecently(map.checked)}">
						{#if isVerifiedRecently(map.checked) === 'verified'}
							Reviewed
						{:else if isVerifiedRecently(map.checked) === 'stale'}
							Needs review
						{:else}
							Draft
						{/if}
					</span>
				</div>

				<div class="card-body">
					<div class="location origin">{map.routeStart}</div>
					<div class="path-arrow">to</div>
					<div class="location destination">{map.routeEnd}</div>
				</div>

				<div class="card-footer">
					<span>Updated: {formatDate(map.updated)}</span>
					{#if map.checked}
						<span>• Checked: {formatDate(map.checked)}</span>
					{/if}
				</div>
			</a>
		{:else}
			<div class="empty-state">No maps found.</div>
		{/each}
	</div>
</div>

<style>
	/* Mobile-First Base Styles */
	:global(body) {
		margin: 0;
		background-color: #404c55;
		background-image: radial-gradient(#2b343c, #404c55);
		color: #0f172a;
		font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
	}

	.page-wrapper {
		padding: 1rem;
		max-width: 800px;
		margin: 0 auto;
	}

	.main-header {
		margin-bottom: 1.5rem;
		position: fixed;
		height: 80px;
		top: 0;
		left: 0;
		box-sizing: border-box;
		width: 100%;
		background: #3c6f79;
		color: #ebebeb;
		z-index: 100;
	}

	.brand-container {
		padding: 15px;
		display: flex;
		width: 100%;
		align-items: center;
		box-sizing: border-box;
		gap: 1rem;
	}

	.main-logo {
		height: 52px;
		width: auto;
		display: block;
	}

	.search-input {
		width: 100%;
		max-width: 500px;
		font-family: 'urwgothic';
		margin: auto;
		height: 40px;
		padding: 0.8rem 1rem;
		text-align: center;
		border-radius: 30px;
		border: none;
		font-size: 1rem;
		transition: all 0.3s ease;
		box-sizing: border-box; /* Ensures padding doesn't break width */
		outline: none;
	}

	.search-input:hover {
		box-shadow: rgba(0, 0, 0, 0.46);
	}

	.list-container {
		display: flex;
		flex-direction: column;
		gap: 1rem;
		padding: 90px 1rem 1rem 1rem;
		isolation: isolate;
		z-index: 1;
	}

	@media (max-width: 600px) {
		.list-container {
			padding-top: 90px;
			gap: 0.75rem;
			padding-left: 0.5rem;
			padding-right: 0.5rem;
		}

		.card {
			padding: 10px 12px;
			margin: 0;
			border-radius: 4px;
		}

		.location {
			font-size: 1rem;
			margin-bottom: 0.1rem;
			font-weight: 500;
		}

		.path-arrow {
			color: #fff;
			font-size: 0.8rem;
			margin: 0.1rem 0;
			line-height: 0.75;
		}

		.card-footer {
			font-size: 0.6rem;
			line-height: 0.8rem;
			padding-bottom: 2px;
		}
	}

	.card {
		display: block;
		background: #3c6f79;
		padding: 1.25rem;
		border-radius: 35px;
		text-decoration: none;
		color: #4fd1d1;
		text-shadow: 2px 1px 10px rgba(0, 0, 0, 0.29);
		box-shadow: 0 1px 3px rgba(0, 0, 0, 0.482);
		transition: transform 0.1s ease;
		cursor: pointer;
	}

	.card:hover {
		transform: scale(1.01);
		z-index: 5;
	}

	.card:active {
		transform: scale(0.99); /* Tactile feedback for mobile */
	}

	.card-top {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 0.75rem;
	}

	.route-id {
		font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
		font-weight: 800;
		color: #fff;
		background: #2d2d2d;
		text-shadow: none;
		padding: 0.2rem 0.5rem;
		border-radius: 6px;
	}

	.card-body {
		margin-bottom: 1rem;
	}

	.location {
		font-family: 'urwgothic';
		font-size: 1.23rem;
		font-weight: 700;
	}

	.path-arrow {
		color: #fff;
		font-family: 'urwgothic';
		font-size: 0.9rem;
		margin: 0.2rem 0;
	}

	.card-footer {
		font-size: 0.7rem;
		color: #e2ebeb;
		text-transform: uppercase;
		font-weight: 600;
		letter-spacing: 0.05em;
		padding-top: 0;
	}

	.status-badge {
		font-size: 0.65rem;
		font-weight: 800;
		text-shadow: none;
		text-transform: uppercase;
		padding: 0.3rem 0.6rem;
		border-radius: 999px;
		white-space: nowrap;
	}

	.verified {
		background: #dcfce7;
		color: #166534;
	}

	.stale {
		background: #fef3c7;
		color: #92400e;
		border: 1px solid #fcd34d;
	}

	.draft {
		background: #f1f5f9;
		color: #475569;
	}

	.button-link {
		text-decoration: none;
		border: none;
		background: #3c6f79;
		max-width: 360px;
		padding: 8px 25px;
		margin: auto;
		text-align: center;
		border-radius: 25px;
		width: auto;
		color: #e2ebeb;
		transition: 0.3s all ease;
		font-weight: 620;
	}

	.button-link:hover {
		background: #2b2b2b;
	}
</style>
