<script setup lang="ts">
/**
 * LoginView – Nexus Mission Control V2 Galaxy Theme
 *
 * Vollbild-Login mit GalaxyBackground, Glassmorphismus,
 * und Consistent Branding.
 * Zeigt verbleibende Login-Versuche und Rate-Limit-Countdown.
 */
import { onMounted, onUnmounted, ref, computed } from 'vue'
import { Mail, LockKeyhole, Command, Eye, EyeOff, Clock, AlertTriangle } from '@lucide/vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '../stores/auth'
import GalaxyBackground from '../components/background/GalaxyBackground.vue'

const auth = useAuthStore()
const route = useRoute()
const router = useRouter()

const email = ref('')
const password = ref('')
const error = ref('')
const showPassword = ref(false)

// Rate-limit countdown timer
const countdown = ref(0)
let countdownInterval: ReturnType<typeof setInterval> | null = null

const countdownText = computed(() => {
  if (countdown.value <= 0) return ''
  const m = Math.floor(countdown.value / 60)
  const s = countdown.value % 60
  return `${m}:${s.toString().padStart(2, '0')}`
})

function startCountdown(seconds: number) {
  stopCountdown()
  countdown.value = seconds
  countdownInterval = setInterval(() => {
    countdown.value = Math.max(0, countdown.value - 1)
    if (countdown.value <= 0 && countdownInterval) {
      clearInterval(countdownInterval)
      countdownInterval = null
    }
  }, 1000)
}

function stopCountdown() {
  if (countdownInterval) {
    clearInterval(countdownInterval)
    countdownInterval = null
  }
}

onUnmounted(() => stopCountdown())

async function submit() {
  error.value = ''
  try {
    await auth.login(email.value.trim(), password.value)
    const target = typeof route.query.redirect === 'string' && route.query.redirect.startsWith('/')
      ? route.query.redirect
      : '/dashboard'
    await router.replace(target)
  } catch (reason) {
    if (reason instanceof Error) {
      error.value = reason.message

      // The reset timestamp also describes the rolling attempt window after a
      // regular 401. Only present a lock countdown once the backend has
      // actually exhausted the remaining attempts.
      if (auth.isRateLimited && auth.retryAfterSeconds > 0) {
        startCountdown(auth.retryAfterSeconds)
      }
    } else {
      error.value = 'Login fehlgeschlagen.'
    }
  }
}
</script>

<template>
  <div class="login-container">
    <GalaxyBackground />

    <div class="login-content">
      <!-- Branding -->
      <div class="login-brand">
        <div class="brand-icon">
          <Command :size="22" />
        </div>
        <span class="brand-name">NEXUS</span>
        <span class="brand-sub">Mission Control · Noveria</span>
      </div>

      <!-- Login Card -->
      <div class="login-card">
        <div class="card-header">
          <span class="eyebrow">AUTHENTIFIZIERUNG</span>
          <h1>Anmelden</h1>
          <p>Gib deine Zugangsdaten ein, um auf das Mission Control zuzugreifen.</p>
        </div>

        <form @submit.prevent="submit" class="login-form">
          <div class="field">
            <label for="email">
              <Mail :size="14" />
              <span>E-Mail</span>
            </label>
            <input
              id="email"
              v-model="email"
              type="email"
              autocomplete="username"
              required
              maxlength="120"
              placeholder="name@noveria.net"
              class="field-input"
              :disabled="auth.isRateLimited"
            />
          </div>

          <div class="field">
            <label for="password">
              <LockKeyhole :size="14" />
              <span>Passwort</span>
            </label>
            <div class="password-wrap">
              <input
                id="password"
                v-model="password"
                :type="showPassword ? 'text' : 'password'"
                autocomplete="current-password"
                required
                minlength="10"
                maxlength="200"
                placeholder="••••••••••"
                class="field-input"
                :disabled="auth.isRateLimited"
              />
              <button
                type="button"
                class="toggle-pw"
                @click="showPassword = !showPassword"
                :aria-label="showPassword ? 'Passwort verbergen' : 'Passwort anzeigen'"
                tabindex="-1"
                :disabled="auth.isRateLimited"
              >
                <Eye v-if="!showPassword" :size="16" />
                <EyeOff v-else :size="16" />
              </button>
            </div>
          </div>

          <!-- Error display with remaining attempts -->
          <div v-if="error" class="error-box" role="alert">
            <div class="error-main">
              <AlertTriangle v-if="auth.isRateLimited && countdown > 0" :size="16" class="error-icon" />
              <span>{{ error }}</span>
            </div>
            <div v-if="auth.remainingAttempts !== null && auth.remainingAttempts > 0" class="attempts-remaining">
              <LockKeyhole :size="12" />
              <span>{{ auth.remainingAttempts }} {{ auth.remainingAttempts === 1 ? 'Versuch verbleibend' : 'Versuche verbleibend' }}</span>
            </div>
            <div v-if="auth.isRateLimited && countdown > 0" class="countdown-bar">
              <Clock :size="12" />
              <span>Entsperrt in {{ countdownText }}</span>
            </div>
          </div>

          <button type="submit" class="submit-btn" :disabled="auth.loading || !email || !password || auth.isRateLimited">
            <LockKeyhole :size="15" />
            <template v-if="auth.loading">Anmelden…</template>
            <template v-else-if="auth.isRateLimited && countdown > 0">Gesperrt ({{ countdownText }})</template>
            <template v-else>Anmelden</template>
          </button>
        </form>

        <footer class="card-footer">
          <span class="lock-icon">🔒</span>
          <span>Gesicherte Sitzung · Refresh-Token im HTTP-only Cookie</span>
        </footer>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.login-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 32px;
  max-width: 420px;
  width: 90%;
  animation: login-fade-in 0.5s ease-out;
}

@keyframes login-fade-in {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Branding ─────────────────────── */
.login-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
}

.brand-icon {
  width: 56px;
  height: 56px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #4f7cff, #b557f6);
  box-shadow: 0 0 32px -4px rgba(124, 108, 255, 0.6);
  color: #fff;
  margin-bottom: 4px;
}

.brand-name {
  font-family: 'Space Grotesk', sans-serif;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 0.2em;
  color: #ece9ff;
  line-height: 1;
}

.brand-sub {
  font-size: 12px;
  color: #6f6aa0;
  letter-spacing: 0.05em;
  margin-top: 2px;
}

/* ── Login Card ────────────────────── */
.login-card {
  width: 100%;
  background: linear-gradient(160deg, rgba(20, 17, 48, 0.88), rgba(14, 12, 32, 0.88));
  border: 1px solid rgba(150, 140, 255, 0.12);
  border-radius: 20px;
  padding: 32px 28px;
  backdrop-filter: blur(16px);
  box-shadow: 0 0 0 1px rgba(124, 108, 255, 0.06), 0 24px 80px -12px rgba(0, 0, 0, 0.5);
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.card-header {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.eyebrow {
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: 0.15em;
  color: #7c6cff;
  text-transform: uppercase;
}

.card-header h1 {
  font-family: 'Space Grotesk', sans-serif;
  font-size: 24px;
  font-weight: 700;
  margin: 0;
  color: #ece9ff;
  letter-spacing: -0.02em;
}

.card-header p {
  margin: 0;
  font-size: 13px;
  color: #6f6aa0;
  line-height: 1.5;
}

/* ── Form ──────────────────────────── */
.login-form {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  font-weight: 600;
  color: #a8a3d6;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.field-input {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid rgba(150, 140, 255, 0.12);
  border-radius: 12px;
  background: rgba(10, 9, 24, 0.55);
  color: #ece9ff;
  font-size: 14px;
  font-family: 'Manrope', sans-serif;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  box-sizing: border-box;
}

.field-input::placeholder {
  color: #4a4680;
}

.field-input:focus {
  border-color: rgba(124, 108, 255, 0.5);
  box-shadow: 0 0 0 3px rgba(124, 108, 255, 0.12);
}

.field-input:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.password-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.password-wrap .field-input {
  padding-right: 44px;
}

.toggle-pw {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: #6f6aa0;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.toggle-pw:hover {
  background: rgba(124, 108, 255, 0.08);
  color: #a8a3d6;
}

.toggle-pw:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* ── Error Box ─────────────────────── */
.error-box {
  padding: 12px 14px;
  border-radius: 10px;
  background: rgba(244, 63, 94, 0.1);
  border: 1px solid rgba(244, 63, 94, 0.2);
  color: #fda4af;
  font-size: 12.5px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.error-main {
  display: flex;
  align-items: center;
  gap: 6px;
}

.error-icon {
  flex-shrink: 0;
  color: #fda4af;
}

.attempts-remaining {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 11.5px;
  color: #f9a8d4;
  padding: 4px 8px;
  background: rgba(244, 63, 94, 0.06);
  border-radius: 6px;
  width: fit-content;
}

.countdown-bar {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 11.5px;
  color: #fb923c;
  padding: 4px 8px;
  background: rgba(251, 146, 60, 0.08);
  border-radius: 6px;
  width: fit-content;
}

/* ── Submit Button ─────────────────── */
.submit-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  padding: 13px 20px;
  border: none;
  border-radius: 14px;
  background: linear-gradient(135deg, #4f7cff, #7c6cff, #b557f6);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  font-family: 'Manrope', sans-serif;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.15s, box-shadow 0.2s;
  box-shadow: 0 0 24px -6px rgba(124, 108, 255, 0.5);
}

.submit-btn:hover:not(:disabled) {
  opacity: 0.92;
  transform: translateY(-1px);
  box-shadow: 0 0 32px -4px rgba(124, 108, 255, 0.65);
}

.submit-btn:active:not(:disabled) {
  transform: translateY(0);
}

.submit-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  box-shadow: none;
}

/* ── Footer ────────────────────────── */
.card-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-top: 4px;
  border-top: 1px solid rgba(150, 140, 255, 0.08);
  font-size: 10.5px;
  color: #6f6aa0;
}

.lock-icon {
  font-size: 13px;
}
</style>
