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

	const { data }: PageProps = $props();

	const languageNames: Record<string, string> = {
		en: 'English',
		fr: 'French',
		es: 'Spanish',
		it: 'Italian',
		de: 'German',
		pt: 'Portuguese',
		ja: 'Japanese',
		zh: 'Chinese',
		ko: 'Korean'
	};

	const lang = (code: string) => languageNames[code] ?? code.toUpperCase();

	const fmt = (iso: string) =>
		new Intl.DateTimeFormat('en-GB', {
			year: 'numeric',
			month: 'long',
			day: 'numeric'
		}).format(new Date(iso));
</script>

<div class="page">
	<header class="page-header">
		<p class="form-eyebrow">Reading</p>
		<h1 class="form-title">Articles</h1>
	</header>

	{#if data.articles.length > 0}
		<ul class="article-list" role="list">
			{#each data.articles as article}
				<li class="article-item">
					<a href="/app/articles/{article.id}" class="article-link">
						<div class="article-meta">
							<span class="label-md meta-lang">{lang(article.target_language)}</span>
							{#each article.target_complexities as level}
								<span class="complexity-tag label-md">{level}</span>
							{/each}
						</div>
						<h2 class="article-title">{article.target_title}</h2>
						<p class="article-source">{article.source_title}</p>
						<time class="article-date label-md" datetime={article.published_at}>{fmt(article.published_at)}</time>
					</a>
				</li>
			{/each}
		</ul>
	{:else}
		<div class="empty-state">
			<p class="empty-heading">No articles yet</p>
			<p class="empty-body">Generate content to see articles here.</p>
		</div>
	{/if}
</div>

<style>
	.page {
		max-width: 52rem;
		margin: 0 auto;
		padding: var(--space-8) var(--space-6);
		display: flex;
		flex-direction: column;
		gap: var(--space-6);
	}

	/* --- Article list --- */

	.article-list {
		list-style: none;
		display: flex;
		flex-direction: column;
		gap: var(--space-3);
	}

	.article-item {
		background-color: var(--color-surface-container-low);
		border-radius: var(--radius-lg);
		transition: background-color var(--duration-fast) var(--ease-standard);
	}

	.article-item:hover {
		background-color: var(--color-surface-container);
	}

	.article-link {
		display: flex;
		flex-direction: column;
		gap: var(--space-2);
		padding: var(--space-5) var(--space-6);
		text-decoration: none;
		color: inherit;
	}

	/* --- Article metadata --- */

	.article-meta {
		display: flex;
		align-items: center;
		gap: var(--space-2);
		flex-wrap: wrap;
	}

	.meta-lang {
		color: var(--color-primary);
	}

	.complexity-tag {
		color: var(--color-on-surface-variant);
		background-color: var(--color-surface-container-high);
		padding: 0.15em 0.5em;
		border-radius: var(--radius-full);
	}

	/* --- Article content --- */

	.article-title {
		font-family: var(--font-display);
		font-size: var(--text-headline-md);
		font-weight: var(--weight-semibold);
		line-height: var(--leading-snug);
		color: var(--color-on-surface);
	}

	.article-source {
		font-family: var(--font-body);
		font-size: var(--text-body-sm);
		color: var(--color-on-surface-variant);
		font-style: italic;
	}

	.article-date {
		color: var(--color-on-surface-variant);
		margin-top: var(--space-1);
	}

	/* --- Empty state --- */

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: var(--space-3);
		padding: var(--space-10) var(--space-6);
		text-align: center;
		background-color: var(--color-surface-container-low);
		border-radius: var(--radius-xl);
	}

	.empty-heading {
		font-family: var(--font-display);
		font-size: var(--text-title-lg);
		font-weight: var(--weight-semibold);
		color: var(--color-on-surface);
	}

	.empty-body {
		font-size: var(--text-body-md);
		color: var(--color-on-surface-variant);
		max-width: 20rem;
	}

	@media (max-width: 640px) {
		.page {
			padding: var(--space-6) var(--space-4);
		}

		.article-link {
			padding: var(--space-4);
		}
	}
</style>
