<script lang="ts">
	import { enhance } from '$app/forms';
	import { Button } from '$lib/components/ui/button';
	import { FormField, FormMessage, LoadingSpinner } from '$lib/components/auth';

	let { data, form } = $props();

	let password = $state('');
	let confirmPassword = $state('');
	let loading = $state(false);

	const isInvite = data?.isInvite;
	const email = data?.email;
</script>

<svelte:head>
	<title>{isInvite ? 'Set Your Password' : 'Reset Password'} | Monaco USA</title>
</svelte:head>

<div class="relative flex min-h-screen items-center justify-center overflow-hidden px-4 py-12">
	<!-- Background decorative elements -->
	<div class="absolute inset-0 -z-10">
		<div
			class="absolute -left-40 -top-40 h-80 w-80 rounded-full bg-monaco-500/20 blur-3xl"
		></div>
		<div
			class="absolute -right-40 top-1/3 h-96 w-96 rounded-full bg-monaco-400/10 blur-3xl"
		></div>
	</div>

	<div class="w-full max-w-md">
		<!-- Logo and Branding -->
		<div class="mb-8 text-center">
			<a href="/" class="inline-flex flex-col items-center">
				<div class="mb-4 overflow-hidden rounded-2xl bg-white/90 p-2 shadow-xl backdrop-blur-sm">
					<img
						src="/MONACOUSA-Flags_376x376.png"
						alt="Monaco USA"
						class="h-16 w-16 object-contain"
					/>
				</div>
				<h1 class="text-2xl font-bold text-slate-900">
					Monaco <span class="text-monaco-600">USA</span>
				</h1>
			</a>
		</div>

		<div class="glass-card p-8">
			<div class="space-y-6">
				<div class="text-center">
					<h2 class="text-xl font-semibold text-slate-900">
						{isInvite ? 'Welcome to Monaco USA!' : 'Reset your password'}
					</h2>
					<p class="mt-1 text-sm text-slate-500">
						{#if isInvite}
							Set a password to activate your account
						{:else}
							Enter a new password for your account
						{/if}
					</p>
					{#if email}
						<p class="mt-2 text-sm text-slate-600">{email}</p>
					{/if}
				</div>

				{#if form?.error}
					<FormMessage type="error" message={form.error} />
				{/if}

				{#if form?.success}
					<FormMessage type="success" message={form.success} />
					<div class="text-center">
						<a
							href="/login"
							class="inline-flex items-center gap-2 rounded-lg bg-monaco-600 px-4 py-2 text-sm font-medium text-white hover:bg-monaco-700"
						>
							Sign in with new password
						</a>
					</div>
				{:else}
					<form
						method="POST"
						use:enhance={() => {
							loading = true;
							return async ({ update }) => {
								loading = false;
								await update();
							};
						}}
						class="space-y-4"
					>
						<FormField
							label="New password"
							name="password"
							type="password"
							placeholder="Enter new password"
							required
							disabled={loading}
							bind:value={password}
							autocomplete="new-password"
						/>

						<FormField
							label="Confirm new password"
							name="confirm_password"
							type="password"
							placeholder="Confirm new password"
							required
							disabled={loading}
							bind:value={confirmPassword}
							autocomplete="new-password"
						/>

						<p class="text-xs text-slate-500">Password must be at least 8 characters long.</p>

						<Button type="submit" variant="monaco" size="lg" class="w-full" disabled={loading}>
							{#if loading}
								<LoadingSpinner size="sm" class="mr-2" />
								Resetting password...
							{:else}
								Reset password
							{/if}
						</Button>
					</form>
				{/if}
			</div>
		</div>
	</div>
</div>
