<script lang="ts">
import { m } from '$lib/paraglide/messages.js';
import { enhance } from '$app/forms';
import PersonEditForm from './PersonEditForm.svelte';
import PersonEditSaveBar from './PersonEditSaveBar.svelte';
import NameHistoryEditCard from './NameHistoryEditCard.svelte';
import PersonDangerZone from './PersonDangerZone.svelte';

let { data, form } = $props();
const person = $derived(data.person);
</script>

<div class="mx-auto max-w-2xl px-4 py-8">
	<!-- Back link -->
	<div class="mb-6">
		<a
			href="/persons/{person.id}"
			class="group mb-4 inline-flex items-center text-xs font-bold tracking-widest text-ink-2 uppercase transition-colors hover:text-ink"
		>
			<svg
				class="mr-2 h-4 w-4 transform transition-transform group-hover:-translate-x-1"
				fill="none"
				stroke="currentColor"
				viewBox="0 0 24 24"
			>
				<path
					stroke-linecap="round"
					stroke-linejoin="round"
					stroke-width="2"
					d="M10 19l-7-7m0 0l7-7m-7 7h18"
				/>
			</svg>
			{person.displayName}
		</a>
		<h1 class="font-serif text-3xl text-ink">{m.person_edit_heading()}</h1>
	</div>

	{#if form?.updateError}
		<div class="mb-6 rounded border border-red-200 bg-red-50 p-4 text-red-700">
			{form.updateError}
		</div>
	{/if}

	<form id="person-edit-form" method="POST" use:enhance>
		<div class="rounded-sm border border-line bg-surface p-6 shadow-sm">
			<h2 class="mb-5 text-xs font-bold tracking-widest text-ink-3 uppercase">
				{m.persons_section_details()}
			</h2>
			<PersonEditForm person={person} />
		</div>
	</form>

	<NameHistoryEditCard aliases={data.aliases} canWrite={true} aliasError={form?.aliasError} />

	<PersonDangerZone person={person} form={form} />

	<PersonEditSaveBar discardHref="/persons/{person.id}" formId="person-edit-form" />
</div>
