<script lang="ts">
import { m } from '$lib/paraglide/messages.js';

type Person = { id: string; firstName: string; lastName: string };

type Doc = {
	id: string;
	title?: string | null;
	originalFilename?: string | null;
	documentDate?: string | null;
	sender?: Person | null;
	receivers?: Person[] | null;
	filePath?: string | null;
	contentType?: string | null;
};

type Props = {
	doc: Doc;
	canWrite: boolean;
	canAnnotate: boolean;
	fileUrl: string;
	annotateMode: boolean;
};

let { doc, canWrite, canAnnotate, fileUrl, annotateMode = $bindable() }: Props = $props();

const isPdf = $derived(!!doc.filePath && doc.contentType?.startsWith('application/pdf'));

const receiverDisplay = $derived.by(() => {
	const receivers = doc.receivers ?? [];
	if (receivers.length === 0) return null;
	const shown = receivers.slice(0, 2);
	const extra = receivers.length - shown.length;
	const names = shown.map((r) => `${r.firstName} ${r.lastName}`).join(', ');
	return extra > 0 ? `${names} +${extra}` : names;
});

const compactMeta = $derived.by(() => {
	const parts: string[] = [];
	if (doc.documentDate) {
		parts.push(
			new Intl.DateTimeFormat('de-DE', {
				day: 'numeric',
				month: 'numeric',
				year: 'numeric'
			}).format(new Date(doc.documentDate + 'T12:00:00'))
		);
	}
	if (doc.sender) {
		const senderName = `${doc.sender.firstName} ${doc.sender.lastName}`;
		const receiver = receiverDisplay;
		parts.push(receiver ? `${senderName} → ${receiver}` : senderName);
	} else if (receiverDisplay) {
		parts.push(`→ ${receiverDisplay}`);
	}
	return parts.join(' · ');
});
</script>

<div
	class="z-20 flex shrink-0 items-center justify-between border-b border-line bg-surface px-3 py-3 shadow-sm sm:px-6"
	data-topbar
>
	<!-- Left: back + title -->
	<div class="flex min-w-0 items-center gap-4 overflow-hidden">
		<a
			href="/"
			class="group flex shrink-0 items-center gap-2 font-sans text-sm font-medium text-ink-2 transition-colors hover:text-ink"
		>
			<div
				class="flex h-8 w-8 items-center justify-center rounded-full bg-canvas transition-colors group-hover:bg-accent"
			>
				<img
					src="/degruyter-icons/Simple/Medium-24px/SVG/Action/Arrow/Arrow-Left-MD.svg"
					alt=""
					aria-hidden="true"
					class="h-4 w-4"
				/>
			</div>
			<span class="hidden sm:inline">{m.btn_back()}</span>
		</a>

		<div class="min-w-0 border-l border-line pl-4">
			<h1
				class="truncate font-serif text-base leading-tight text-ink"
				title={doc.title ?? doc.originalFilename ?? ''}
			>
				{doc.title || doc.originalFilename}
			</h1>
			{#if compactMeta}
				<p class="truncate font-sans text-xs text-ink-2" title={compactMeta}>
					{compactMeta}
				</p>
			{/if}
		</div>
	</div>

	<!-- Right: actions -->
	<div class="ml-4 flex shrink-0 items-center gap-2 font-sans">
		{#if canAnnotate && isPdf}
			<button
				onclick={() => (annotateMode = !annotateMode)}
				aria-label={annotateMode ? m.doc_panel_annotate_stop() : m.doc_panel_annotate()}
				class="flex items-center gap-1.5 rounded px-3 py-1.5 font-sans text-xs font-medium transition {annotateMode
					? 'bg-primary text-primary-fg'
					: 'border border-primary text-ink hover:bg-primary hover:text-primary-fg'}"
			>
				<img
					src="/degruyter-icons/Simple/Medium-24px/SVG/Action/Note/Note-Add-MD.svg"
					alt=""
					aria-hidden="true"
					class="h-4 w-4 {annotateMode ? 'invert' : ''}"
				/>
				<span class="hidden sm:inline"
					>{annotateMode ? m.doc_panel_annotate_stop() : m.doc_panel_annotate()}</span
				>
			</button>
		{/if}

		{#if canWrite}
			<a
				href="/documents/{doc.id}/edit"
				aria-label={m.btn_edit()}
				class="flex items-center gap-2 rounded border border-primary bg-transparent px-3 py-1.5 text-xs font-medium text-ink transition hover:bg-primary hover:text-primary-fg"
			>
				<img
					src="/degruyter-icons/Simple/Medium-24px/SVG/Action/Edit-Content-MD.svg"
					alt=""
					aria-hidden="true"
					class="h-4 w-4"
				/>
				<span class="hidden sm:inline">{m.btn_edit()}</span>
			</a>
		{/if}

		{#if doc.filePath}
			<a
				href={fileUrl}
				download={doc.originalFilename}
				class="rounded border border-transparent bg-muted p-1.5 text-ink transition hover:bg-accent"
				title={m.doc_download_title()}
			>
				<img
					src="/degruyter-icons/Simple/Medium-24px/SVG/Action/Download-MD.svg"
					alt=""
					aria-hidden="true"
					class="h-5 w-5"
				/>
			</a>
		{/if}
	</div>
</div>
