<script lang="ts">
	import { formatLanguage } from '$lib/formatters';
	import type { PageProps } from './$types';
	import ArticlePreview from './ArticlePreview.svelte';

	const { data }: PageProps = $props();
	const { job, fullAudioUrl, translatedArticle } = data;

	const fmt = (iso: string | null | undefined) => {
		if (!iso) return null;
		return new Intl.DateTimeFormat('en-GB', {
			year: 'numeric',
			month: 'short',
			day: 'numeric',
			hour: '2-digit',
			minute: '2-digit'
		}).format(new Date(iso));
	};

	const isTerminal = job.status === 'succeeded' || job.status === 'failed';
</script>

<div class="page">
	<nav class="breadcrumb">
		<a href="/app/jobs" class="link">← Jobs</a>
	</nav>

	<header class="page-header">
		<div class="header-top">
			<h1 class="form-title">Job Details</h1>
			<span class="status-badge" data-status={job.status}>{job.status}</span>
		</div>
		<p class="job-id">{job.id}</p>
	</header>

	<dl class="meta-grid">
		<div class="meta-item">
			<dt class="field-label">Complexity</dt>
			<dd class="meta-value">{job.complexity_level}</dd>
		</div>
		<div class="meta-item">
			<dt class="field-label">Created</dt>
			<dd class="meta-value">{fmt(job.created_at)}</dd>
		</div>
		{#if job.started_at}
			<div class="meta-item">
				<dt class="field-label">Started</dt>
				<dd class="meta-value">{fmt(job.started_at)}</dd>
			</div>
		{/if}
		{#if job.completed_at}
			<div class="meta-item">
				<dt class="field-label">Completed</dt>
				<dd class="meta-value">{fmt(job.completed_at)}</dd>
			</div>
		{/if}
	</dl>

	{#if job.status === 'failed' && job.error_message}
		<div class="alert alert-error" role="alert">
			<strong>Job failed:</strong>
			{job.error_message}
		</div>
	{:else if !isTerminal}
		<div class="pending-notice">
			<div class="spinner" aria-hidden="true"></div>
			<p>This job is <strong>{job.status}</strong>. Refresh to check for updates.</p>
		</div>
	{/if}

	<ArticlePreview article={translatedArticle} {fullAudioUrl} />
</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);
	}

	/* --- Breadcrumb --- */

	.breadcrumb {
		font-family: var(--font-label);
		font-size: var(--text-label-lg);
	}

	/* --- Header --- */

	.header-top {
		display: flex;
		align-items: center;
		gap: var(--space-3);
		flex-wrap: wrap;
	}

	.job-id {
		font-family: var(--font-label);
		font-size: var(--text-label-md);
		color: var(--color-on-surface-variant);
		margin-top: var(--space-1);
		word-break: break-all;
	}

	/* --- Status badge --- */

	.status-badge {
		display: inline-block;
		padding: 0.2em 0.75em;
		border-radius: var(--radius-full);
		font-family: var(--font-label);
		font-size: var(--text-label-md);
		font-weight: var(--weight-medium);
		letter-spacing: var(--tracking-wide);
		text-transform: uppercase;
		background-color: var(--color-secondary-container);
		color: var(--color-on-secondary-container);
	}

	.status-badge[data-status='completed'] {
		background-color: var(--color-primary-container);
		color: var(--color-on-primary-container);
	}

	.status-badge[data-status='failed'] {
		background-color: color-mix(in srgb, #b3261e 12%, var(--color-surface));
		color: #b3261e;
	}

	/* --- Metadata grid --- */

	.meta-grid {
		display: grid;
		grid-template-columns: repeat(auto-fill, minmax(10rem, 1fr));
		gap: var(--space-3) var(--space-5);
		padding: var(--space-4);
		background-color: var(--color-surface-container-low);
		border-radius: var(--radius-lg);
	}

	.meta-item {
		display: flex;
		flex-direction: column;
		gap: var(--space-1);
	}

	.meta-value {
		font-family: var(--font-display);
		font-size: var(--text-body-md);
		font-weight: var(--weight-medium);
		color: var(--color-on-surface);
	}

	/* --- Pending notice --- */

	.pending-notice {
		display: flex;
		align-items: center;
		gap: var(--space-3);
		padding: var(--space-4);
		background-color: var(--color-secondary-container);
		border-radius: var(--radius-lg);
		font-family: var(--font-label);
		font-size: var(--text-body-sm);
		color: var(--color-on-secondary-container);
	}

	.spinner {
		width: 1.25rem;
		height: 1.25rem;
		border: 2px solid var(--color-outline-variant);
		border-top-color: var(--color-primary);
		border-radius: 50%;
		animation: spin 0.9s linear infinite;
		flex-shrink: 0;
	}

	@keyframes spin {
		to {
			transform: rotate(360deg);
		}
	}

	/* --- Responsive --- */

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