<template>
  <div class="error-page">
    <!-- Aurora Background -->
    <div class="aurora-bg">
      <div class="aurora-gradient aurora-1"></div>
      <div class="aurora-gradient aurora-2"></div>
      <div class="aurora-gradient aurora-3"></div>
    </div>

    <!-- Error Container -->
    <div class="error-container glass">
      <div class="error-icon">
        <component :is="errorIcon" :size="80" />
      </div>

      <h1 class="error-title">{{ errorTitle }}</h1>
      <p class="error-message">{{ errorMessage }}</p>

      <div class="error-details" v-if="showDetails">
        <code>{{ error }}</code>
      </div>

      <div class="error-actions">
        <button @click="goHome" class="btn btn-primary">
          <Home :size="20" />
          <span>Ir al Menú Principal</span>
        </button>

        <button @click="goAuth" class="btn btn-secondary">
          <LogIn :size="20" />
          <span>Autenticación Authentik</span>
        </button>
      </div>

      <button @click="toggleDetails" class="btn-text">
        {{ showDetails ? 'Ocultar' : 'Ver' }} detalles técnicos
      </button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { AlertCircle, Wifi, WifiOff, Home, LogIn } from 'lucide-vue-next'

const props = defineProps({
  error: {
    type: Object,
    required: true
  }
})

const showDetails = ref(false)

const errorIcon = computed(() => {
  const statusCode = props.error?.statusCode
  if (statusCode === 404) return AlertCircle
  if (statusCode >= 500) return Wifi
  if (!navigator.onLine) return WifiOff
  return AlertCircle
})

const errorTitle = computed(() => {
  const statusCode = props.error?.statusCode
  if (statusCode === 404) return '404 - Página No Encontrada'
  if (statusCode === 403) return '403 - Acceso Denegado'
  if (statusCode === 401) return '401 - No Autorizado'
  if (statusCode >= 500) return '500 - Error del Servidor'
  if (!navigator.onLine) return 'Sin Conexión'
  return `Error ${statusCode || 'Desconocido'}`
})

const errorMessage = computed(() => {
  const statusCode = props.error?.statusCode
  if (statusCode === 404) {
    return 'La página que buscas no existe o ha sido movida.'
  }
  if (statusCode === 403 || statusCode === 401) {
    return 'Necesitas autenticarte para acceder a esta página. Por favor, inicia sesión con Authentik.'
  }
  if (statusCode >= 500) {
    return 'Ocurrió un error en el servidor. Por favor, intenta más tarde.'
  }
  if (!navigator.onLine) {
    return 'No hay conexión a Internet. Verifica tu conexión y vuelve a intentar.'
  }
  return props.error?.message || 'Ha ocurrido un error inesperado.'
})

const toggleDetails = () => {
  showDetails.value = !showDetails.value
}

const goHome = () => {
  clearError({ redirect: '/' })
}

const goAuth = () => {
  window.location.href = 'https://authentik.nucleoriofrio.com'
}
</script>

<style scoped>
.error-page {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(135deg, #0f172a 0%, #1e1b4b 100%);
  overflow: auto;
}

/* Aurora Background */
.aurora-bg {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
}

.aurora-gradient {
  position: absolute;
  border-radius: 50%;
  filter: blur(120px);
  opacity: 0.4;
  animation: float 20s ease-in-out infinite;
}

.aurora-1 {
  width: 600px;
  height: 600px;
  background: radial-gradient(circle, rgba(59, 130, 246, 0.6) 0%, transparent 70%);
  top: -10%;
  left: -10%;
  animation-delay: 0s;
}

.aurora-2 {
  width: 500px;
  height: 500px;
  background: radial-gradient(circle, rgba(139, 92, 246, 0.6) 0%, transparent 70%);
  bottom: -10%;
  right: -10%;
  animation-delay: 7s;
}

.aurora-3 {
  width: 550px;
  height: 550px;
  background: radial-gradient(circle, rgba(236, 72, 153, 0.5) 0%, transparent 70%);
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  animation-delay: 14s;
}

@keyframes float {
  0%, 100% {
    transform: translate(0, 0) scale(1);
  }
  33% {
    transform: translate(50px, -50px) scale(1.1);
  }
  66% {
    transform: translate(-50px, 50px) scale(0.9);
  }
}

/* Error Container */
.error-container {
  position: relative;
  z-index: 1;
  max-width: 600px;
  width: 100%;
  padding: 40px;
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 24px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
  text-align: center;
  animation: slideIn 0.6s ease-out;
}

@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.error-icon {
  display: flex;
  justify-content: center;
  margin-bottom: 24px;
  color: var(--accent-primary, #3b82f6);
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.8;
    transform: scale(1.05);
  }
}

.error-title {
  font-size: 2rem;
  font-weight: 700;
  margin: 0 0 16px 0;
  background: linear-gradient(45deg, #3b82f6, #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.error-message {
  font-size: 1.1rem;
  color: rgba(255, 255, 255, 0.8);
  line-height: 1.6;
  margin: 0 0 32px 0;
}

.error-details {
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 24px;
  max-height: 200px;
  overflow: auto;
  text-align: left;
}

.error-details code {
  font-family: 'Courier New', monospace;
  font-size: 0.85rem;
  color: rgba(255, 255, 255, 0.9);
  word-break: break-all;
}

.error-actions {
  display: flex;
  gap: 16px;
  justify-content: center;
  margin-bottom: 24px;
  flex-wrap: wrap;
}

.btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  text-decoration: none;
  font-family: inherit;
}

.btn-primary {
  background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
  color: white;
  box-shadow: 0 4px 20px rgba(59, 130, 246, 0.4);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 30px rgba(59, 130, 246, 0.6);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.12);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.18);
  transform: translateY(-2px);
}

.btn-text {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.6);
  font-size: 0.9rem;
  cursor: pointer;
  text-decoration: underline;
  transition: color 0.3s ease;
  font-family: inherit;
  padding: 8px;
}

.btn-text:hover {
  color: rgba(255, 255, 255, 0.9);
}

/* Responsive */
@media (max-width: 640px) {
  .error-container {
    padding: 30px 20px;
  }

  .error-icon :deep(svg) {
    width: 60px;
    height: 60px;
  }

  .error-title {
    font-size: 1.5rem;
  }

  .error-message {
    font-size: 1rem;
  }

  .error-actions {
    flex-direction: column;
    width: 100%;
  }

  .btn {
    width: 100%;
    justify-content: center;
  }
}

/* Theme support */
:root {
  --accent-primary: #3b82f6;
  --accent-secondary: #8b5cf6;
}

[data-theme="dark"] {
  --accent-primary: #60a5fa;
  --accent-secondary: #a78bfa;
}
</style>
