<script lang="ts">
import * as m from '$lib/paraglide/messages.js';
import { getLocale } from '$lib/paraglide/runtime.js';
import { formatMCDate } from '$lib/shared/utils/date.js';
import type { components } from '$lib/generated/api';
import ContributorStack from '$lib/shared/primitives/ContributorStack.svelte';

type TranscriptionQueueItemDTO = components['schemas']['TranscriptionQueueItemDTO'];

interface Props {
	docs: TranscriptionQueueItemDTO[];
}

let { docs }: Props = $props();

function reviewedPct(doc: TranscriptionQueueItemDTO): number {
	if (doc.annotationCount === 0) return 0;
	return Math.round((doc.reviewedBlockCount / doc.annotationCount) * 100);
}
</script>

{#if docs.length > 0}
	<div
		class="flex flex-col gap-3 rounded-sm border border-brand-mint bg-brand-mint/10 p-4 transition-shadow hover:shadow-sm"
	>
		<div>
			<div class="mb-1">
				<h3 class="font-sans text-xs font-bold tracking-widest text-ink uppercase">
					{m.mission_control_ready_heading()}
				</h3>
			</div>
			<p class="text-xs font-semibold text-ink-2">
				{m.mission_control_ready_subtitle({ count: docs.length })}
			</p>
		</div>
		<ul class="space-y-1">
			{#each docs as doc (doc.id)}
				<li>
					<a
						href="/documents/{doc.id}"
						class="flex min-h-[44px] flex-col justify-center rounded px-1 py-2 hover:bg-brand-mint/20 focus-visible:ring-2 focus-visible:ring-focus-ring focus-visible:ring-offset-2 focus-visible:outline-none"
					>
						<span class="font-serif text-sm text-ink">{doc.title}</span>
						<div class="mt-0.5 flex items-center gap-2">
							{#if doc.documentDate}
								<span class="text-xs text-ink-3">{formatMCDate(doc.documentDate, getLocale())}</span
								>
							{/if}
							{#if doc.textedBlockCount > 0}
								<span class="text-xs font-semibold text-ink">
									{m.mission_control_reviewed_pct({ pct: reviewedPct(doc) })}
								</span>
							{/if}
						</div>
						<div class="mt-1">
							<ContributorStack contributors={doc.contributors} hasMore={doc.hasMoreContributors} />
						</div>
					</a>
				</li>
			{/each}
		</ul>
	</div>
{:else}
	<div
		class="flex min-h-[120px] flex-col items-center justify-center rounded-sm border border-dashed border-brand-mint bg-brand-mint/5 p-6 text-center"
	>
		<p class="text-xs text-ink-3">{m.mission_control_ready_empty()}</p>
		<a
			href="/enrich?filter=NEEDS_SEGMENTATION&next=1"
			class="mt-2 inline-flex items-center rounded-sm border border-ink px-3 py-2 text-xs font-semibold text-ink transition-colors hover:bg-ink hover:text-primary-fg focus-visible:ring-2 focus-visible:ring-focus-ring focus-visible:ring-offset-2 focus-visible:outline-none"
		>
			{m.mission_control_ready_empty_cta()}
		</a>
	</div>
{/if}
