<script lang="ts">
	import { onMount } from "svelte";
	import { goto } from "$app/navigation";

	interface Item {
		id: number;
		title: string;
		date: string;
		impact: string;
		imageUri: string | null;
	}

	let item = $state<Item | null>(null);

	onMount(() => {
		const storedItem = sessionStorage.getItem("selectedItem");
		if (storedItem) {
			item = JSON.parse(storedItem);
		}
	});

	function navigateToReport() {
		if (item) {
			goto(`/report?productName=${encodeURIComponent(item.title)}`);
		}
	}
</script>

<div class="page-container">
	{#if item}
		<div class="safe-area">
			<div class="header">
				<button
					class="back-button"
					onclick={() => window.history.back()}
					aria-label="Go back"
				>
					<svg
						xmlns="http://www.w3.org/2000/svg"
						viewBox="0 0 320 512"
						fill="white"
					>
						<path
							d="M224 480c-8.188 0-16.38-3.125-22.62-9.375l-192-192c-12.5-12.5-12.5-32.75 0-45.25l192-192c12.5-12.5 32.75-12.5 45.25 0s12.5 32.75 0 45.25L77.25 256l169.4 169.4c12.5 12.5 12.5 32.75 0 45.25C240.4 476.9 232.2 480 224 480z"
						/>
					</svg>
				</button>
				<h1 class="page-title">Scan Report</h1>
				<p class="subtitle">Detailed analysis</p>
			</div>

			<div class="scroll-content">
				<div class="card">
					<div class="header-row">
						<div class="title-section">
							<p class="date">{item.date}</p>
							<h2 class="product-title">{item.title}</h2>
						</div>
						<div
							class="impact-badge"
							class:high-impact={item.impact === "High"}
							class:low-impact={item.impact === "Low"}
						>
							{item.impact} Impact
						</div>
					</div>

					<div class="analysis-box">
						<svg
							xmlns="http://www.w3.org/2000/svg"
							viewBox="0 0 512 512"
							fill={item.impact === "High"
								? "#ef4444"
								: "#22c55e"}
							class="alert-icon"
						>
							<path
								d="M256 512A256 256 0 1 0 256 0a256 256 0 1 0 0 512zm0-384c13.3 0 24 10.7 24 24V264c0 13.3-10.7 24-24 24s-24-10.7-24-24V152c0-13.3 10.7-24 24-24zM224 352a32 32 0 1 1 64 0 32 32 0 1 1 -64 0z"
							/>
						</svg>
						<div class="analysis-text">
							<h3>Analysis Result</h3>
							<p>
								{#if item.impact === "High"}
									This item takes 450+ years to decompose.
									Consider switching to sustainable
									alternatives immediately.
								{:else}
									This item is eco-friendly or easily
									recyclable. Good choice!
								{/if}
							</p>
						</div>
					</div>

					<div class="alternatives-section">
						<h3 class="alternatives-title">
							Recommended Alternatives
						</h3>
						<div class="alternatives-scroll">
							<div class="alternative-card glass">
								<div class="alt-header">
									<svg
										xmlns="http://www.w3.org/2000/svg"
										viewBox="0 0 384 512"
										fill="#60a5fa"
									>
										<path
											d="M192 512C86 512 0 426 0 320C0 228.8 130.2 57.7 166.6 11.7C172.6 4.2 181.5 0 191.1 0h1.8c9.6 0 18.5 4.2 24.5 11.7C253.8 57.7 384 228.8 384 320c0 106-86 192-192 192zM96 336c0-8.8-7.2-16-16-16s-16 7.2-16 16c0 61.9 50.1 112 112 112 8.8 0 16-7.2 16-16s-7.2-16-16-16c-44.2 0-80-35.8-80-80z"
										/>
									</svg>
									<span class="rating">★ 4.9</span>
								</div>
								<h4 class="alt-name">Glass Bottle</h4>
								<p class="alt-price">$2.49</p>
							</div>

							<div class="alternative-card boxed">
								<div class="alt-header">
									<svg
										xmlns="http://www.w3.org/2000/svg"
										viewBox="0 0 448 512"
										fill="#a78bfa"
									>
										<path
											d="M384 480H64c-35.3 0-64-28.7-64-64V96C0 60.7 28.7 32 64 32h320c35.3 0 64 28.7 64 64v320c0 35.3-28.7 64-64 64z"
										/>
									</svg>
									<span class="rating">★ 4.7</span>
								</div>
								<h4 class="alt-name">Boxed Water</h4>
								<p class="alt-price">$1.89</p>
							</div>

							<div class="alternative-card aluminum">
								<div class="alt-header">
									<svg
										xmlns="http://www.w3.org/2000/svg"
										viewBox="0 0 512 512"
										fill="#9ca3af"
									>
										<path
											d="M0 192c0-35.3 28.7-64 64-64c.5 0 1.1 0 1.6 0C73 91.5 105.3 64 144 64c15 0 29 4.1 40.9 11.2C198.2 49.6 225.1 32 256 32s57.8 17.6 71.1 43.2C339 68.1 353 64 368 64c38.7 0 71 27.5 78.4 64c.5 0 1.1 0 1.6 0c35.3 0 64 28.7 64 64c0 11.7-3.1 22.6-8.6 32H8.6C3.1 214.6 0 203.7 0 192zm0 91.4C0 268.3 12.3 256 27.4 256H484.6c15.1 0 27.4 12.3 27.4 27.4c0 70.5-44.4 130.7-106.7 154.1L403.5 452c-2 16-15.6 28-31.8 28H140.2c-16.1 0-29.8-12-31.8-28l-1.8-14.4C44.4 414.1 0 353.9 0 283.4z"
										/>
									</svg>
									<span class="rating">★ 4.5</span>
								</div>
								<h4 class="alt-name">Aluminum</h4>
								<p class="alt-price">$1.29</p>
							</div>
						</div>
					</div>

					{#if item.impact !== "Low"}
						<button
							class="report-button"
							onclick={navigateToReport}
						>
							<svg
								xmlns="http://www.w3.org/2000/svg"
								viewBox="0 0 448 512"
								fill="#ef4444"
							>
								<path
									d="M64 32C64 14.3 49.7 0 32 0S0 14.3 0 32V64 368 480c0 17.7 14.3 32 32 32s32-14.3 32-32V352l64.3-16.1c41.1-10.3 84.6-5.5 122.5 13.4c44.2 22.1 95.5 24.8 141.7 7.4l34.7-13c12.5-4.7 20.8-16.6 20.8-30V66.1c0-23-24.2-38-44.8-27.7l-9.6 4.8c-46.3 23.2-100.8 23.2-147.1 0c-35.1-17.6-75.4-22-113.5-12.5L64 48V32z"
								/>
							</svg>
							<span>Report Greenwashing</span>
						</button>
					{/if}

					{#if item.imageUri}
						<img
							src={item.imageUri}
							alt="Scanned product"
							class="product-image"
						/>
					{/if}
				</div>
			</div>
		</div>
	{:else}
		<div class="loading">
			<p>Loading...</p>
		</div>
	{/if}
</div>

<style>
	.page-container {
		width: 100%;
		height: 100vh;
		background-color: #0f172a;
		overflow-y: auto;
	}

	.loading {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100vh;
		color: white;
	}

	.safe-area {
		padding-top: 50px;
		padding-bottom: 120px;
	}

	.header {
		padding: 24px;
		padding-top: 30px;
		position: relative;
	}

	.back-button {
		background: none;
		border: none;
		cursor: pointer;
		padding: 8px;
		margin-bottom: 16px;
	}

	.back-button svg {
		width: 24px;
		height: 24px;
	}

	.page-title {
		color: white;
		font-size: 34px;
		font-weight: 800;
		letter-spacing: -0.5px;
		margin: 0;
	}

	.subtitle {
		color: #94a3b8;
		font-size: 14px;
		margin: 4px 0 0 0;
	}

	.scroll-content {
		padding: 24px;
		padding-bottom: 140px;
	}

	.card {
		background-color: rgba(30, 41, 59, 0.9);
		padding: 20px;
		border-radius: 24px;
		border: 1px solid #334155;
	}

	.header-row {
		display: flex;
		justify-content: space-between;
		align-items: flex-start;
		gap: 10px;
	}

	.title-section {
		flex: 1;
	}

	.date {
		color: #94a3b8;
		font-size: 14px;
		margin: 0 0 8px 0;
	}

	.product-title {
		color: white;
		font-size: 32px;
		font-weight: bold;
		margin: 0;
	}

	.impact-badge {
		padding: 8px 16px;
		border-radius: 12px;
		font-size: 16px;
		font-weight: bold;
		flex-shrink: 0;
	}

	.high-impact {
		background-color: rgba(239, 68, 68, 0.2);
		color: #ef4444;
	}

	.low-impact {
		background-color: rgba(34, 197, 94, 0.2);
		color: #22c55e;
	}

	.analysis-box {
		margin-top: 32px;
		padding: 20px;
		background-color: #1e293b;
		border-radius: 16px;
		display: flex;
		align-items: center;
		gap: 16px;
	}

	.alert-icon {
		width: 32px;
		height: 32px;
		flex-shrink: 0;
	}

	.analysis-text {
		flex: 1;
	}

	.analysis-text h3 {
		color: white;
		font-weight: bold;
		font-size: 16px;
		margin: 0 0 4px 0;
	}

	.analysis-text p {
		color: #94a3b8;
		margin: 0;
		font-size: 14px;
	}

	.alternatives-section {
		margin-top: 32px;
	}

	.alternatives-title {
		color: white;
		font-weight: bold;
		font-size: 18px;
		margin: 0 0 16px 0;
	}

	.alternatives-scroll {
		display: flex;
		gap: 12px;
		overflow-x: auto;
		padding-bottom: 8px;
	}

	.alternatives-scroll::-webkit-scrollbar {
		height: 6px;
	}

	.alternatives-scroll::-webkit-scrollbar-track {
		background: #1e293b;
		border-radius: 3px;
	}

	.alternatives-scroll::-webkit-scrollbar-thumb {
		background: #334155;
		border-radius: 3px;
	}

	.alternative-card {
		min-width: 140px;
		background-color: rgba(30, 41, 59, 0.8);
		padding: 16px;
		border-radius: 16px;
		border: 1px solid #334155;
	}

	.alt-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 8px;
	}

	.alt-header svg {
		width: 24px;
		height: 24px;
	}

	.rating {
		font-weight: bold;
		color: #fbbf24;
		font-size: 14px;
	}

	.alt-name {
		font-weight: bold;
		color: white;
		font-size: 16px;
		margin: 0 0 4px 0;
	}

	.alt-price {
		color: #94a3b8;
		margin: 0;
		font-size: 14px;
	}

	.report-button {
		margin-top: 32px;
		background-color: rgba(239, 68, 68, 0.2);
		padding: 16px;
		border-radius: 12px;
		border: none;
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 8px;
		cursor: pointer;
		transition: all 0.3s ease;
		width: 100%;
	}

	.report-button:hover {
		background-color: rgba(239, 68, 68, 0.3);
	}

	.report-button svg {
		width: 20px;
		height: 20px;
	}

	.report-button span {
		color: #ef4444;
		font-weight: bold;
	}

	.product-image {
		width: 100%;
		height: 300px;
		border-radius: 16px;
		margin-top: 24px;
		object-fit: cover;
	}
</style>
