<script lang="ts">
	import { type ComicFilter, type FullComicFragment } from '$gql/graphql';
	import { CategoryLabel, CensorshipLabel, LanguageLabel, RatingLabel } from '$lib/Enums';
	import { href } from '$lib/Navigation';
	import { formatListSize, joinText } from '$lib/Utils';
	import AssociationPill from '$lib/pills/AssociationPill.svelte';
	import TagPill from '$lib/pills/TagPill.svelte';
	import { formatDistance, formatISO9075 } from 'date-fns';
	import Header from './DetailsHeader.svelte';
	import Section from './DetailsSection.svelte';

	let { comic }: { comic: FullComicFragment } = $props();

	const now = Date.now();
	const updatedDate = new Date(comic.updatedAt);
	const createdDate = new Date(comic.createdAt);

	const title = joinText([
		comic.category ? CategoryLabel[comic.category] : '',
		formatListSize('page', comic.pages.length)
	]);

	function filterFor(filter: keyof ComicFilter, id: number | string) {
		return href('comics', { filter: { include: { [filter]: { all: [id] } } } });
	}
</script>

<div class="flex flex-col gap-4 text-sm">
	<Header {title}>
		<a
			href={`/archives/${comic.archive.id}`}
			class="btn-slate"
			title="Go to Archive"
			aria-label="Go to Archive"
		>
			<span class="icon-base icon-[material-symbols--folder-zip]"></span>
		</a>
	</Header>

	<div class="grid grid-cols-3 gap-4">
		{#if comic.language}
			<Section title="Language">
				<span>{LanguageLabel[comic.language]}</span>
			</Section>
		{/if}
		{#if comic.censorship}
			<Section title="Censorship">
				<span>{CensorshipLabel[comic.censorship]}</span>
			</Section>
		{/if}
		{#if comic.rating}
			<Section title="Rating">
				<span>{RatingLabel[comic.rating]}</span>
			</Section>
		{/if}
	</div>

	<div class="grid grid-cols-3 gap-4">
		{#if comic.date}
			<Section title="Released">
				<span>{formatISO9075(new Date(comic.date), { representation: 'date' })}</span>
			</Section>
		{/if}
		<Section title="Created">
			<span title={formatISO9075(createdDate)}>
				{formatDistance(createdDate, now, { addSuffix: true })}
			</span>
		</Section>
		<Section title="Updated">
			<span title={formatISO9075(updatedDate)}>
				{formatDistance(updatedDate, now, { addSuffix: true })}
			</span>
		</Section>
	</div>

	{#if comic.url}
		<Section title="URL">
			<a
				class="ellipsis-nowrap transition-colors hover:text-white hover:underline"
				rel="noreferrer"
				href={comic.url}>{comic.url}</a
			>
		</Section>
	{/if}

	{#if comic.artists.length}
		<Section title="Artists">
			{#each comic.artists as { id, name } (id)}
				<a href={filterFor('artists', id)}>
					<AssociationPill {name} type="artist" />
				</a>
			{/each}
		</Section>
	{/if}
	{#if comic.circles.length}
		<Section title="Circles">
			{#each comic.circles as { id, name } (id)}
				<a href={filterFor('circles', id)}>
					<AssociationPill {name} type="circle" />
				</a>
			{/each}
		</Section>
	{/if}
	{#if comic.characters.length}
		<Section title="Characters">
			{#each comic.characters as { id, name } (id)}
				<a href={filterFor('characters', id)}>
					<AssociationPill {name} type="character" />
				</a>
			{/each}
		</Section>
	{/if}
	{#if comic.worlds.length}
		<Section title="Worlds">
			{#each comic.worlds as { id, name } (id)}
				<a href={filterFor('worlds', id)}>
					<AssociationPill {name} type="world" />
				</a>
			{/each}
		</Section>
	{/if}
	{#if comic.tags.length}
		<Section title="Tags">
			{#each comic.tags as { id, name, description } (id)}
				<a href={filterFor('tags', id)}>
					<TagPill {name} {description} />
				</a>
			{/each}
		</Section>
	{/if}
</div>
