<script setup lang="ts">
const auth = useAuthStore()
const router = useRouter()

const address = ref('')
const step = ref<'input' | 'challenge' | 'signing' | 'success'>('input')
const errorMessage = ref('')

async function handleLogin() {
  if (!address.value.trim()) {
    errorMessage.value = 'Veuillez entrer votre adresse Duniter'
    return
  }

  errorMessage.value = ''
  step.value = 'challenge'

  try {
    step.value = 'signing'
    await auth.login(address.value.trim())
    step.value = 'success'
    setTimeout(() => {
      router.push('/')
    }, 800)
  } catch (err: any) {
    errorMessage.value = err?.data?.detail || err?.message || 'Erreur lors de la connexion'
    step.value = 'input'
  }
}

const steps = computed(() => [
  { label: 'Adresse SS58', icon: 'i-lucide-user', done: step.value !== 'input' },
  { label: 'Challenge', icon: 'i-lucide-shield', done: step.value === 'signing' || step.value === 'success' },
  { label: 'Signature', icon: 'i-lucide-key', done: step.value === 'success' },
  { label: 'Connecte', icon: 'i-lucide-check', done: false },
])

const activeStepIndex = computed(() => {
  switch (step.value) {
    case 'input': return 0
    case 'challenge': return 1
    case 'signing': return 2
    case 'success': return 3
    default: return 0
  }
})

onMounted(() => {
  if (auth.isAuthenticated) {
    router.push('/')
  }
})
</script>

<template>
  <div class="login-page">
    <div class="login-card">
      <!-- Logo -->
      <div class="login-card__header">
        <div class="login-card__logo">
          <UIcon name="i-lucide-gavel" class="text-xl" />
        </div>
        <h1 class="login-card__title">Connexion</h1>
        <p class="login-card__subtitle">ğ(Decision) · Duniter V2 · Ed25519</p>
      </div>

      <!-- Steps indicator -->
      <div class="login-steps">
        <div
          v-for="(s, i) in steps"
          :key="i"
          class="login-step"
          :class="{
            'login-step--done': s.done,
            'login-step--active': i === activeStepIndex,
          }"
        >
          <div class="login-step__dot">
            <UIcon v-if="s.done" name="i-lucide-check" class="text-xs" />
            <span v-else class="login-step__num">{{ i + 1 }}</span>
          </div>
          <span class="login-step__label">{{ s.label }}</span>
        </div>
      </div>

      <!-- Input -->
      <div class="login-card__field">
        <label class="login-card__label">Adresse Duniter (SS58)</label>
        <input
          v-model="address"
          type="text"
          class="login-card__input"
          placeholder="5GrwvaEF5zXb26Fz9rcQpDWS57Ct..."
          :disabled="auth.loading || step !== 'input'"
          @keydown.enter="handleLogin"
        />
      </div>

      <!-- Error -->
      <div v-if="errorMessage || auth.error" class="login-card__error">
        <UIcon name="i-lucide-alert-circle" class="text-sm flex-shrink-0" />
        <span>{{ errorMessage || auth.error }}</span>
      </div>

      <!-- Success -->
      <div v-if="step === 'success'" class="login-card__success">
        <UIcon name="i-lucide-check-circle" class="text-sm flex-shrink-0" />
        <span>Connecte. Redirection...</span>
      </div>

      <!-- Button -->
      <button
        class="login-card__btn"
        :disabled="!address.trim() || step === 'success' || auth.loading"
        @click="handleLogin"
      >
        <UIcon v-if="auth.loading" name="i-lucide-loader-2" class="animate-spin text-sm" />
        <UIcon v-else name="i-lucide-log-in" class="text-sm" />
        <span>{{ auth.loading ? 'Verification...' : 'Se connecter' }}</span>
      </button>

      <!-- Note -->
      <p class="login-card__note">
        Aucun mot de passe. Authentification par signature cryptographique.
      </p>
    </div>
  </div>
</template>

<style scoped>
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 70vh;
  padding: 2rem 1rem;
}

.login-card {
  width: 100%;
  max-width: 24rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.login-card__header {
  text-align: center;
}

.login-card__logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 3rem;
  height: 3rem;
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--mood-accent-text);
  background: var(--mood-accent);
  border-radius: 8px;
  margin-bottom: 0.75rem;
}

.login-card__title {
  font-size: 1.375rem;
  font-weight: 700;
  color: var(--mood-text);
  margin: 0;
}

.login-card__subtitle {
  font-size: 0.75rem;
  color: var(--mood-text-muted);
  margin-top: 0.25rem;
  letter-spacing: 0.02em;
}

/* Steps */
.login-steps {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.25rem;
  padding: 0.75rem 0;
}

.login-step {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
  flex: 1;
}

.login-step__dot {
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.625rem;
  font-weight: 700;
  border: 1.5px solid var(--mood-border);
  color: var(--mood-text-muted);
  background: var(--mood-surface);
  transition: all 0.2s ease;
}

.login-step--active .login-step__dot {
  border-color: var(--mood-accent);
  color: var(--mood-accent);
  box-shadow: 0 0 0 3px var(--mood-accent-soft);
}

.login-step--done .login-step__dot {
  border-color: var(--mood-success);
  background: var(--mood-success);
  color: white;
}

.login-step__num {
  font-size: 0.5625rem;
}

.login-step__label {
  font-size: 0.5625rem;
  font-weight: 500;
  color: var(--mood-text-muted);
  text-align: center;
  letter-spacing: 0.02em;
}

.login-step--active .login-step__label {
  color: var(--mood-accent);
  font-weight: 600;
}

/* Field */
.login-card__field {
  display: flex;
  flex-direction: column;
  gap: 0.375rem;
}

.login-card__label {
  font-size: 0.6875rem;
  font-weight: 600;
  color: var(--mood-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.login-card__input {
  width: 100%;
  padding: 0.625rem 0.75rem;
  font-size: 0.8125rem;
  font-family: ui-monospace, SFMono-Regular, 'Cascadia Code', monospace;
  color: var(--mood-text);
  background: var(--mood-input-bg, var(--mood-surface));
  border: 1px solid var(--mood-input-border, var(--mood-border));
  border-radius: 4px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.login-card__input:focus {
  border-color: var(--mood-input-focus, var(--mood-accent));
  box-shadow: 0 0 0 2px var(--mood-accent-soft);
}

.login-card__input:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.login-card__input::placeholder {
  color: var(--mood-text-muted);
  opacity: 0.5;
}

/* Messages */
.login-card__error {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--mood-error);
  background: rgba(196, 43, 43, 0.08);
  border-radius: 4px;
}

.login-card__success {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--mood-success);
  background: rgba(24, 132, 59, 0.08);
  border-radius: 4px;
}

/* Button */
.login-card__btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.625rem 1rem;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--mood-accent-text);
  background: var(--mood-accent);
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: opacity 0.15s ease;
  letter-spacing: 0.01em;
}

.login-card__btn:hover:not(:disabled) {
  opacity: 0.88;
}

.login-card__btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Note */
.login-card__note {
  text-align: center;
  font-size: 0.6875rem;
  color: var(--mood-text-muted);
  opacity: 0.7;
}
</style>
