<script setup lang="ts">
import { ref } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Eye, EyeOff } from 'lucide-vue-next'

const auth = useAuthStore()
const email = ref('')
const password = ref('')
const showPassword = ref(false)

async function handleLogin() {
  if (!email.value || !password.value) return
  await auth.login({ email: email.value, password: password.value })
}
</script>

<template>
  <div class="flex min-h-svh flex-col items-center justify-center bg-background p-6">
    <Card class="w-full max-w-[400px]">
      <CardHeader class="text-center pb-4">
        <div class="flex aspect-square size-12 items-center justify-center rounded-xl bg-primary text-primary-foreground mx-auto mb-3">
          <span class="font-bold text-xl">A</span>
        </div>
        <CardTitle class="text-2xl font-bold tracking-tight">Alpha</CardTitle>
        <CardDescription class="text-xs mt-1">KAPPA Hub</CardDescription>
      </CardHeader>

      <CardContent>
        <form @submit.prevent="handleLogin" class="flex flex-col gap-4">
          <div class="space-y-1.5">
            <label class="text-xs font-medium text-muted-foreground uppercase tracking-wider">Email</label>
            <Input v-model="email" type="email" placeholder="ricardo@..." autocomplete="email" />
          </div>
          <div class="space-y-1.5">
            <label class="text-xs font-medium text-muted-foreground uppercase tracking-wider">Contraseña</label>
            <div class="relative">
              <Input
                v-model="password"
                :type="showPassword ? 'text' : 'password'"
                placeholder="••••••••"
                autocomplete="current-password"
                class="pr-10"
              />
              <button
                type="button"
                class="absolute right-2 top-1/2 -translate-y-1/2 text-muted-foreground hover:text-foreground"
                @click="showPassword = !showPassword"
              >
                <EyeOff v-if="showPassword" class="size-4" />
                <Eye v-else class="size-4" />
              </button>
            </div>
          </div>

          <div v-if="auth.error" class="text-xs text-destructive bg-destructive/10 rounded-md px-3 py-2">
            {{ auth.error }}
          </div>

          <Button type="submit" class="w-full mt-2" :disabled="auth.loading">
            {{ auth.loading ? 'Ingresando...' : 'Iniciar sesión' }}
          </Button>
        </form>

        <p class="text-center text-[11px] text-muted-foreground/40 mt-6">
          kappa.lambdaanalytics.co
        </p>
      </CardContent>
    </Card>
  </div>
</template>
