<script lang="ts">
	import { Button } from 'flowbite-svelte';
	import type { PageProps } from './$types';
	import { EditOutline, CloseOutline } from 'flowbite-svelte-icons';
	import AktiEditor from '$lib/components/akti/AktiEditor.svelte';
	import UserDisplay from '$lib/components/UserDisplay.svelte';
	import RatingCard from '$lib/components/rating/RatingCard.svelte';
	import { resolve } from '$app/paths';

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

	let edit = $state(false);
	let canComment = $derived.by(() => {
		if (!data.session) return false;
		if (data.akti.author.id === data.session.user.id) return false;
		return true;
	});
</script>

<div class="flex justify-between">
	<h2>{data.akti?.title} <span class="text-xs text-gray-400">v{data.akti.version}</span></h2>
	{#if data.session?.user?.id === data.akti.author?.id && data.akti.author?.id}
		<Button onclick={() => (edit = !edit)} color={edit ? 'gray' : 'primary'}>
			{#if edit}
				<CloseOutline class="shrink-0 h-6 w-6" />
			{:else}
				<EditOutline class="shrink-0 h-6 w-6 -mr-0.5 ml-0.5" />
			{/if}
		</Button>
	{:else}
		<div class="flex gap-5 items-center">
			<p>gschribe vo:</p>
			<UserDisplay user={data.akti.author} />
		</div>
	{/if}
</div>

{#if edit}
	<AktiEditor akti={data.akti} />
{:else}
	<div class="p-5 my-5 bg-gray-200 rounded-md">
		<h3 class="mb-2">Zämefassig</h3>
		<p>{data.akti.summary}</p>
	</div>
	<!-- eslint-disable-next-line svelte/no-at-html-tags -->
	{@html data.akti.body}

	<div class="mt-10 mb-5 flex justify-between items-center">
		<h3>Kommentär</h3>
		{#if canComment}
			<Button href={resolve('/akti/[aktiId]/comment', { aktiId: data.akti.id })}>
				Ä Kommentar da la
			</Button>
		{/if}
	</div>

	<section class="grid grid-cols-1 gap-5 2xl:grid-cols-3">
		{#each data.ratings as rating (rating.id)}
			<RatingCard {rating} />
		{/each}
	</section>
{/if}
