<script lang="ts">
	import type { PageData } from './$types';
	import { base } from '$app/paths';
	export let data: PageData;

	let searchTerm = '';

	const formatDate = (dateVal: string | null) => {
		if (!dateVal) return '---';
		const d = new Date(dateVal);
		return d.toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: '2-digit' });
	};

	$: filteredMaps = data.maps.filter(
		(m) =>
			m.routeId.toString().includes(searchTerm) ||
			m.routeStart.toLowerCase().includes(searchTerm.toLowerCase()) ||
			m.routeEnd.toLowerCase().includes(searchTerm.toLowerCase())
	);

	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';
	};
</script>

<div class="page-wrapper">
	<header class="main-header">
		<div class="brand-container">
			<img
				src="https://owlboard.info/images/logo/wide_logo.svg"
				alt="OwlBoard Logo"
				class="main-logo"
			/>
			<div class="brand-text">
				<h1>Routes</h1>
			</div>
			<div class="search-container">
				<input
					type="text"
					bind:value={searchTerm}
					placeholder="Search index..."
					class="search-input"
				/>
			</div>
		</div>
	</header>

	<div class="list-container">
		<a
			href="https://owlboard.info"
			class="card external-card"
			target="_blank"
			rel="noopener noreferrer"
		>
			<div class="card-top">
				<span class="route-id system-tag">OWL</span>
				<span class="status-badge external">External Tool</span>
			</div>
			<div class="card-body">
				<div class="location">OwlBoard</div>
				<p class="description">Live Departure Boards and PIS Codes</p>
			</div>
			<div class="card-footer">
				<span>Visit owlboard.info ↗</span>
			</div>
		</a>
		{#each filteredMaps as map (map.routeId)}
			<a href="{base}/map/{map.routeId.toString().padStart(4, '0')}" class="card">
				<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>Created: {formatDate(map.created)}</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;
        top: 0;
        left: 0;
        width: 100%;
        background: #3c6f79;
        color: #ebebeb;
	}

	.brand-container {
        margin: 15px;
		display: flex;
		align-items: center;
		gap: 1rem;
	}

	.main-logo {
		/* Adjust height to fit your logo's aspect ratio */
		height: 48px;
		width: auto;
		display: block;
	}

	.brand-text h1 {
		font-size: 1.5rem;
		font-weight: 900;
		margin: 0;
		line-height: 1;
		letter-spacing: -0.05em;
		font-style: italic;
	}

	.sub-brand {
		font-size: 0.65rem;
		font-weight: 800;
		color: #94a3b8;
		text-transform: uppercase;
		letter-spacing: 0.1em;
		margin: 0.2rem 0 0 0;
	}

	/* Desktop layout adjustment */
	@media (min-width: 640px) {
		.main-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 3rem;
		}

		.brand-container {
			margin-bottom: 0;
		}

		.main-logo {
			height: 56px;
		}
	}

	.search-input {
		width: 100%;
		padding: 0.8rem 1rem;
		border: 2px solid #e2e8f0;
		border-radius: 12px;
		font-size: 1rem;
		box-sizing: border-box; /* Ensures padding doesn't break width */
		outline: none;
	}

	.search-input:focus {
		border-color: #3b82f6;
	}

	/* Card Layout (Mobile) */
	.list-container {
		display: flex;
		flex-direction: column;
		gap: 1rem;
        padding-top: 90px;
	}

	.card {
		display: block;
		background: white;
		padding: 1.25rem;
		border-radius: 16px;
		text-decoration: none;
		color: inherit;
		border: 1px solid #e2e8f0;
		box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
		transition: transform 0.1s ease;
		cursor: pointer;
	}

	.card:active {
		transform: scale(0.98); /* 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: #3b82f6;
		background: #eff6ff;
		padding: 0.2rem 0.5rem;
		border-radius: 6px;
	}

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

	.location {
		font-size: 1.125rem;
		font-weight: 700;
		letter-spacing: -0.02em;
	}

	.path-arrow {
		color: #acc7e8;
		font-size: 0.9rem;
		margin: 0.2rem 0;
	}

	.card-footer {
		font-size: 0.7rem;
		color: #64748b;
		text-transform: uppercase;
		font-weight: 600;
		letter-spacing: 0.05em;
		border-top: 1px solid #f1f5f9;
		padding-top: 0.75rem;
	}

	.status-badge {
		font-size: 0.65rem;
		font-weight: 800;
		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;
	}

	@media (min-width: 640px) {
		.page-wrapper {
			padding: 3rem 1.5rem;
		}

		.main-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}

		.search-input {
			width: 300px;
		}

		.path-arrow {
			display: inline-block;
			margin: 0 0.5rem;
		}

		.card-body {
			display: flex;
			align-items: center;
		}
	}

	.external-card {
		border-left: 4px solid #6366f1;
		background: linear-gradient(to right, #ffffff, #f5f3ff);
	}

	.system-tag {
		background: #eef2ff !important;
		color: #4338ca !important;
	}

	.status-badge.external {
		background: #6366f1;
		color: white;
	}

	.description {
		margin: 0.25rem 0 0 0;
		font-size: 0.9rem;
		color: #64748b;
		font-weight: 500;
	}

	.external-card:hover {
		border-color: #4338ca;
		background: #f5f3ff;
	}
</style>
