---
import Base from '../../layouts/Base.astro';
---
<Base title="Create account — BincioActivity" public={true}>
  <div class="max-w-sm mx-auto mt-16 px-4">
    <h1 class="text-2xl font-bold text-white mb-6 text-center">Create account</h1>

    <form id="register-form" class="space-y-4">
      <div>
        <label class="block text-sm text-zinc-400 mb-1" for="code">Invite code</label>
        <input id="code" name="code" type="text" autocomplete="off"
          class="w-full px-3 py-2 rounded-lg bg-zinc-900 border border-zinc-700 text-white font-mono uppercase tracking-widest placeholder-zinc-500 focus:outline-none focus:border-[--accent]"
          placeholder="XXXXXXXX" maxlength="8" required />
      </div>
      <div>
        <label class="block text-sm text-zinc-400 mb-1" for="handle">Handle</label>
        <input id="handle" name="handle" type="text" autocomplete="username"
          class="w-full px-3 py-2 rounded-lg bg-zinc-900 border border-zinc-700 text-white placeholder-zinc-500 focus:outline-none focus:border-[--accent]"
          placeholder="lowercase, letters and numbers" required />
      </div>
      <div>
        <label class="block text-sm text-zinc-400 mb-1" for="display_name">Display name <span class="text-zinc-600">(optional)</span></label>
        <input id="display_name" name="display_name" type="text"
          class="w-full px-3 py-2 rounded-lg bg-zinc-900 border border-zinc-700 text-white placeholder-zinc-500 focus:outline-none focus:border-[--accent]"
          placeholder="Your Name" />
      </div>
      <div>
        <label class="block text-sm text-zinc-400 mb-1" for="password">Password</label>
        <input id="password" name="password" type="password" autocomplete="new-password"
          class="w-full px-3 py-2 rounded-lg bg-zinc-900 border border-zinc-700 text-white focus:outline-none focus:border-[--accent]"
          minlength="8" required />
        <p class="text-zinc-600 text-xs mt-1">At least 8 characters</p>
      </div>
      <p id="reg-error" class="text-red-400 text-sm hidden"></p>
      <button type="submit"
        class="w-full py-2 rounded-lg bg-[--accent] hover:opacity-90 text-white font-medium transition-opacity">
        Create account
      </button>
    </form>

    <p class="text-center text-zinc-500 text-sm mt-6">
      Already have an account? <a href="/login/" class="text-[--accent] hover:underline">Sign in</a>
    </p>
  </div>
</Base>

<script>
  // Pre-fill invite code from query param
  const code = new URLSearchParams(window.location.search).get('code');
  if (code) {
    const input = document.getElementById('code') as HTMLInputElement;
    if (input) input.value = code.toUpperCase();
  }

  document.getElementById('register-form')?.addEventListener('submit', async (e) => {
    e.preventDefault();
    const form = e.target as HTMLFormElement;
    const errEl = document.getElementById('reg-error')!;
    errEl.classList.add('hidden');

    const body = {
      code:         (form.querySelector('#code') as HTMLInputElement).value.trim().toUpperCase(),
      handle:       (form.querySelector('#handle') as HTMLInputElement).value.trim().toLowerCase(),
      display_name: (form.querySelector('#display_name') as HTMLInputElement).value.trim(),
      password:     (form.querySelector('#password') as HTMLInputElement).value,
    };

    try {
      const r = await fetch('/api/register', {
        method: 'POST',
        credentials: 'include',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(body),
      });
      if (!r.ok) {
        const d = await r.json().catch(() => ({}));
        errEl.textContent = d.detail ?? 'Registration failed';
        errEl.classList.remove('hidden');
        return;
      }
      window.location.href = '/';
    } catch {
      errEl.textContent = 'Could not reach server';
      errEl.classList.remove('hidden');
    }
  });
</script>
