<script lang="ts">
import { enhance } from '$app/forms';
import { untrack } from 'svelte';
import { isoToGerman, handleGermanDateInput } from '$lib/shared/utils/date';
import { m } from '$lib/paraglide/messages.js';

let {
	user,
	form
}: {
	user:
		| {
				firstName?: string;
				lastName?: string;
				birthDate?: string;
				email?: string;
				contact?: string;
		  }
		| null
		| undefined;
	form?: { updateSuccess?: boolean; updateError?: string } | null;
} = $props();

let birthDateDisplay = $state(untrack(() => isoToGerman(user?.birthDate ?? '')));
let birthDateIso = $state(untrack(() => user?.birthDate ?? ''));

function handleBirthDateInput(e: Event) {
	const result = handleGermanDateInput(e);
	birthDateDisplay = result.display;
	birthDateIso = result.iso;
}
</script>

<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.profile_section_personal()}
	</h2>

	{#if form?.updateSuccess}
		<div class="mb-5 rounded border border-green-200 bg-green-50 p-3 text-sm text-green-700">
			{m.profile_saved()}
		</div>
	{/if}
	{#if form?.updateError}
		<div class="mb-5 rounded border border-red-200 bg-red-50 p-3 text-sm text-red-700">
			{form.updateError}
		</div>
	{/if}

	<form method="POST" action="?/updateProfile" use:enhance>
		<div class="space-y-4">
			<label class="block">
				<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
					{m.profile_label_first_name()}
				</span>
				<input
					type="text"
					name="firstName"
					value={user?.firstName ?? ''}
					class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:outline-none focus-visible:ring-2 focus-visible:ring-focus-ring"
				/>
			</label>

			<label class="block">
				<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
					{m.profile_label_last_name()}
				</span>
				<input
					type="text"
					name="lastName"
					value={user?.lastName ?? ''}
					class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:outline-none focus-visible:ring-2 focus-visible:ring-focus-ring"
				/>
			</label>

			<label class="block">
				<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
					{m.profile_label_birth_date()}
				</span>
				<input
					type="text"
					placeholder="TT.MM.JJJJ"
					value={birthDateDisplay}
					oninput={handleBirthDateInput}
					class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:outline-none focus-visible:ring-2 focus-visible:ring-focus-ring"
				/>
				<input type="hidden" name="birthDate" value={birthDateIso} />
			</label>

			<label class="block">
				<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
					{m.profile_label_email()}
				</span>
				<input
					type="email"
					name="email"
					value={user?.email ?? ''}
					class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:outline-none focus-visible:ring-2 focus-visible:ring-focus-ring"
				/>
			</label>

			<label class="block">
				<span class="mb-1 block font-sans text-xs font-bold tracking-widest text-ink-3 uppercase">
					{m.profile_label_contact()}
				</span>
				<textarea
					name="contact"
					rows="3"
					placeholder={m.profile_contact_placeholder()}
					class="w-full rounded-sm border border-line px-3 py-2 font-serif text-sm focus:outline-none focus-visible:ring-2 focus-visible:ring-focus-ring"
					>{user?.contact ?? ''}</textarea
				>
			</label>
		</div>

		<button
			type="submit"
			class="mt-5 rounded-sm bg-primary px-5 py-2 font-sans text-xs font-bold tracking-widest text-primary-fg uppercase transition-opacity hover:opacity-80"
		>
			{m.btn_save()}
		</button>
	</form>
</div>
