<script lang="ts">
  import { onMount } from "svelte";
  import { push } from "svelte-spa-router";
  import {
    hasStoredIdentity,
    loadStoredIdentityMeta,
  } from "../lib/identity-store.js";

  let existing = $state<{
    handle: string;
    server: string;
    primary: string;
    created_at: string;
  } | null>(null);
  let loading = $state(true);

  onMount(async () => {
    if (await hasStoredIdentity()) {
      existing = await loadStoredIdentityMeta();
    }
    loading = false;
  });
</script>

<div class="space-y-8">
  <section>
    <h1 class="text-3xl font-bold text-text">Welcome to kez-chat</h1>
    <p class="mt-2 text-text-secondary">
      A decentralized identity + chat system. Create an account, link your
      online identities, prove who you are without trusting a central server.
    </p>
  </section>

  {#if loading}
    <p class="text-text-muted text-sm">Checking local state…</p>
  {:else if existing}
    <section class="border border-border rounded-lg p-6 bg-surface">
      <p class="text-sm text-text-muted mb-1">Existing account on this device:</p>
      <p class="text-xl font-mono font-semibold text-text">
        {existing.handle}@{existing.server}
      </p>
      <p class="mt-1 text-xs font-mono text-text-muted break-all">
        {existing.primary}
      </p>
      <div class="mt-4 flex gap-3">
        <button
          class="px-4 py-2 bg-accent text-accent-contrast rounded-md hover:bg-accent-dim"
          onclick={() => push("/unlock")}
        >
          Unlock
        </button>
      </div>
    </section>
  {:else}
    <section class="grid sm:grid-cols-2 gap-4">
      <button
        class="text-left border border-border rounded-lg p-6 bg-surface hover:border-accent-dim transition"
        onclick={() => push("/create")}
      >
        <h2 class="text-lg font-semibold text-text">Create a new account</h2>
        <p class="mt-1 text-sm text-text-secondary">
          Generate a fresh key pair, pick a handle, back up your seed phrase.
        </p>
      </button>

      <button
        class="text-left border border-border rounded-lg p-6 bg-surface hover:border-accent-dim transition"
        onclick={() => push("/restore")}
      >
        <h2 class="text-lg font-semibold text-text">Restore from seed</h2>
        <p class="mt-1 text-sm text-text-secondary">
          Have a 64-char hex seed from another device? Paste it to recover
          your identity.
        </p>
      </button>
    </section>
  {/if}

  <section class="text-sm text-text-muted border-t border-border pt-6">
    <p class="font-medium text-text-secondary">What is this?</p>
    <p class="mt-1">
      Your identity is an Ed25519 keypair — not a username + password.
      Account creation makes a handle (<code class="bg-elevated px-1 rounded">tudisco@kez.lat</code>),
      stores your seed locally under a passphrase, and registers your public
      key with this server. There's no email, no recovery flow — keep the
      seed safe.
    </p>
  </section>
</div>
