<template>
  <header class="app-header" :class="{ 'admin-mode': authStore.isAdmin }">
    <div class="header-left">
      <button class="menu-btn-custom" @click="toggleMenu">
        <span class="icon">
            <svg viewBox="0 0 80 75" width="24" height="24">
                <rect width="80" height="15" fill="currentColor" rx="10"></rect>
                <rect y="30" width="80" height="15" fill="currentColor" rx="10"></rect>
                <rect y="60" width="80" height="15" fill="currentColor" rx="10"></rect>
            </svg>
        </span>
      </button>
      <div v-if="authStore.isAdmin" class="admin-badge">ADMIN</div>
      <div v-if="authStore.isDriver" class="driver-badge">CONDUCTOR</div>
      
      <ReportModal :is-open="showReportModal" @close="showReportModal = false" />
      
      <!-- Menu Overlay -->
      <Transition name="overlay-fade">
        <div v-if="showMenu" class="menu-overlay" @click="showMenu = false"></div>
      </Transition>

      <Transition name="menu-slide">
        <div v-if="showMenu" class="menu-dropdown nexus-glass">
          <!-- NEXUS HEADER: User Identity -->
          <div class="nexus-user-section">
            <div class="user-avatar-wrapper">
              <span class="material-icons avatar-icon">{{ authStore.isAuthenticated ? 'account_circle' : 'fingerprint' }}</span>
              <div v-if="authStore.isAuthenticated" class="online-indicator"></div>
            </div>
            <div class="user-info">
              <span class="user-greeting">HOLA,</span>
              <span class="user-name">{{ authStore.isAuthenticated ? authStore.userName : 'INVITADO' }}</span>
            </div>
          </div>

          <div class="menu-scroll-area">
            <!-- GROUP 1: Navegación Principal -->
            <div class="nexus-group">
              <div v-if="authStore.isAuthenticated" class="menu-item-nexus" @click="navigateTo('/profile')">
                  <span class="material-icons">person</span> 
                  <span class="item-text">Mi Perfil</span>
              </div>
              
              <div class="menu-item-nexus" @click="navigateTo('/favorites')">
                  <span class="material-icons">favorite</span> 
                  <span class="item-text">Favoritos</span>
              </div>

              <div v-if="authStore.isAdmin" class="menu-item-nexus admin-item" @click="navigateTo('/admin')">
                  <span class="material-icons">admin_panel_settings</span> 
                  <span class="item-text">Panel Admin</span>
              </div>
              
              <div v-if="authStore.isPromoter" class="menu-item-nexus promoter-item" @click="navigateTo('/promoter')">
                  <span class="material-icons">store</span> 
                  <span class="item-text">Panel Promotor</span>
              </div>

              <div v-if="authStore.isDriver" class="menu-item-nexus driver-item" @click="navigateTo('/driver')">
                  <span class="material-icons">speed</span> 
                  <span class="item-text">Panel Conductor</span>
              </div>
            </div>

            <div class="nexus-divider-clean"></div>

            <!-- GROUP 2: Configuración -->
            <div class="nexus-group">
              <div class="menu-item-nexus" @click="toggleLanguage">
                  <span class="material-icons">translate</span> 
                  <span class="item-text">{{ locale === 'es' ? 'English (EN)' : 'Español (ES)' }}</span>
              </div>

              <div class="menu-item-nexus theme-toggle-nexus">
                  <div class="toggle-left">
                    <span class="material-icons">palette</span>
                    <span class="item-text">Modo Oscuro</span>
                  </div>
                  <ThemeToggle class="theme-switch-btn" />
              </div>
            </div>

            <div class="nexus-divider-clean"></div>

            <!-- GROUP 3: Soporte -->
            <div class="nexus-group">
              <div class="menu-item-nexus report-item-nexus" @click="openReportModal">
                  <span class="material-icons">report_problem</span> 
                  <span class="item-text">Enviar Reporte</span>
              </div>
            </div>
          </div>

          <!-- FOOTER: Auth Action -->
          <div class="nexus-footer">
            <div v-if="!authStore.isAuthenticated" class="login-btn-nexus" @click="navigateTo('/login')">
                <span class="material-icons">login</span> Iniciar Sesión
            </div>
            <div v-else class="logout-wrapper-nexus" @click="handleLogout">
              <button class="nexus-logout-btn">
                <span class="material-icons">power_settings_new</span>
                <span>CERRAR SESIÓN</span>
              </button>
            </div>
          </div>
        </div>
      </Transition>
    </div>
    
    <h1 class="header-title" @click="goToHome">{{ t('header.title') }}</h1>
    
    <div class="header-actions">
      <!-- Buttons moved to side menu for cleaner UI -->
    </div>
  </header>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'
import ReportModal from './ReportModal.vue'
import ThemeToggle from './common/ThemeToggle.vue'

const { t, locale } = useI18n()
const authStore = useAuthStore()
const router = useRouter()
const showMenu = ref(false)
const showReportModal = ref(false)

onMounted(() => {
    // Load saved language preference
    const savedLang = localStorage.getItem('user_lang')
    if (savedLang) {
        locale.value = savedLang
    }
})

const toggleLanguage = () => {
    locale.value = locale.value === 'es' ? 'en' : 'es'
    localStorage.setItem('user_lang', locale.value)
}

defineEmits<{
  feedbackClick: [];
}>();

const toggleMenu = () => {
  showMenu.value = !showMenu.value
};

const navigateTo = (path: string) => {
    router.push(path)
    showMenu.value = false
}

const goToHome = () => {
    router.push('/map')
}

const openReportModal = () => {
    showReportModal.value = true
    showMenu.value = false
}

const handleLogout = () => {
    authStore.logout()
    showMenu.value = false
    router.push('/login')
}

// toggleDarkMode removed as it was unused and causing TS errors
</script>

<style scoped>
.app-header {
  background-color: var(--header-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--header-text);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 20px;
  height: 64px;
  border-bottom: 1px solid var(--border-color);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 2000;
  width: 100%;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.app-header.admin-mode {
    background-color: rgba(30, 41, 59, 0.9);
    border-bottom: 2px solid var(--active-color);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.admin-badge {
    background: linear-gradient(135deg, #ef4444 0%, #b91c1c 100%);
    color: white;
    padding: 4px 10px;
    border-radius: 8px;
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 1px;
    box-shadow: 0 4px 12px rgba(239, 68, 68, 0.3);
}

.driver-badge {
    background: linear-gradient(135deg, #fee715 0%, #facc15 100%);
    color: #101820;
    padding: 4px 10px;
    border-radius: 8px;
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 1px;
    box-shadow: 0 4px 12px rgba(254, 231, 21, 0.3);
}

.header-title {
  font-size: 24px;
  font-weight: 900;
  letter-spacing: -0.02em;
  color: var(--text-primary);
  margin: 0;
  text-align: center;
  grid-column: 2;
  cursor: pointer;
  transition: all 0.3s ease;
}

/* En modo oscuro, resaltar un poco más */
:global(.dark) .header-title {
  color: var(--active-color);
}

.header-title:hover {
  transform: scale(1.05);
}

.header-actions {
  grid-column: 3;
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-button {
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: inherit;
  transition: all 0.2s;
  border-radius: 14px;
}

.header-button:hover {
  background-color: var(--hover-bg);
  transform: translateY(-2px);
}

.report-btn-right {
  background: rgba(251, 191, 36, 0.15);
  border: 1px solid rgba(251, 191, 36, 0.3);
  color: #fbbf24;
  padding: 8px 16px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.report-btn-right:hover {
  background: var(--active-color);
  color: #101820;
  transform: translateY(-2px) scale(1.05);
  box-shadow: 0 4px 15px rgba(254, 231, 21, 0.3);
  border-color: var(--active-color);
}

.report-text {
  font-size: 0.85rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

@media (max-width: 480px) {
  .report-text {
    display: none;
  }
  .report-btn-right {
    padding: 10px;
    border-radius: 50%;
  }
}

.logout-header-btn {
  color: #f87171 !important;
  background: rgba(248, 113, 113, 0.08);
  border: 1px solid rgba(248, 113, 113, 0.15);
  margin-left: 8px;
  box-shadow: 0 0 15px rgba(248, 113, 113, 0.05);
}

.logout-header-btn:hover {
  background: rgba(248, 113, 113, 0.2);
  border-color: rgba(248, 113, 113, 0.4);
  box-shadow: 0 4px 15px rgba(248, 113, 113, 0.2);
  color: #ef4444 !important;
}

.menu-dropdown.nexus-glass {
    position: fixed;
    top: 0;
    left: 0;
    width: 280px;
    height: 100vh;
    background: rgba(15, 23, 42, 0.85); /* Deep Nexus Blue */
    backdrop-filter: blur(25px) saturate(180%);
    -webkit-backdrop-filter: blur(25px) saturate(180%);
    border-right: 1px solid rgba(254, 231, 21, 0.2); /* SIBU Gold Border */
    padding: 0;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    box-shadow: 20px 0 80px rgba(0,0,0,0.8);
}

:global(.light-theme) .menu-dropdown.nexus-glass {
    background: rgba(255, 255, 255, 0.85);
    border-right: 1px solid rgba(16, 24, 32, 0.1);
}

.nexus-user-section {
    padding: 40px 24px 24px;
    background: linear-gradient(to bottom, rgba(254, 231, 21, 0.05), transparent);
    display: flex;
    align-items: center;
    gap: 16px;
    border-bottom: 1px solid var(--border-color);
}

.user-avatar-wrapper {
    position: relative;
    width: 52px;
    height: 52px;
    border-radius: 16px;
    background: linear-gradient(135deg, #fee715 0%, #facc15 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 0 25px rgba(254, 231, 21, 0.4);
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.avatar-icon {
    font-size: 32px;
    color: #101820; /* Icono oscuro sobre fondo oro */
}

.online-indicator {
    position: absolute;
    bottom: -2px;
    right: -2px;
    width: 14px;
    height: 14px;
    background: #4ade80;
    border: 3px solid var(--header-bg);
    border-radius: 50%;
}

.user-info {
    display: flex;
    flex-direction: column;
}

.user-greeting {
    font-size: 0.7rem;
    font-weight: 800;
    color: var(--text-secondary);
    letter-spacing: 1px;
}

.user-name {
    font-size: 1.1rem;
    font-weight: 900;
    color: var(--active-color);
    text-transform: uppercase;
}

.menu-scroll-area {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.nexus-group {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.menu-item-nexus {
    padding: 10px 16px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 14px;
    cursor: pointer;
    color: var(--text-primary);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    border: 1px solid transparent;
}

.menu-item-nexus:hover {
    background: var(--hover-bg);
    border-color: var(--border-color);
    transform: translateX(5px);
}

.menu-item-nexus .material-icons {
    font-size: 22px;
    color: var(--text-secondary);
    transition: color 0.3s;
}

.menu-item-nexus:hover .material-icons {
    color: var(--active-color);
}

.item-text {
    font-weight: 700;
    font-size: 0.95rem;
}

.admin-item { color: #f87171; }
.promoter-item { color: #818cf8; }
.driver-item { color: #fbbf24; }

.nexus-divider-clean {
    height: 1px;
    background: linear-gradient(to right, transparent, var(--border-color), transparent);
    margin: 12px 16px;
}

.theme-toggle-nexus {
    justify-content: space-between;
}

.toggle-left {
    display: flex;
    align-items: center;
    gap: 16px;
}

.report-item-nexus {
    color: #fbbf24;
    background: rgba(251, 191, 36, 0.05);
}

.nexus-footer {
    padding: 24px;
    border-top: 1px solid var(--border-color);
}

.login-btn-nexus {
    background: var(--active-color);
    color: #101820;
    padding: 14px;
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    font-weight: 900;
    cursor: pointer;
    transition: all 0.3s;
    box-shadow: 0 10px 20px rgba(254, 231, 21, 0.2);
}

.login-btn-nexus:hover {
    transform: translateY(-2px);
    box-shadow: 0 15px 30px rgba(254, 231, 21, 0.3);
}

.nexus-logout-btn {
    width: 100%;
    background: rgba(239, 68, 68, 0.1);
    border: 1px solid rgba(239, 68, 68, 0.2);
    color: #ef4444;
    padding: 14px;
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    font-weight: 800;
    cursor: pointer;
    transition: all 0.3s;
}

.nexus-logout-btn:hover {
    background: #ef4444;
    color: white;
}

.menu-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(4px);
    z-index: 9999;
}

/* Animations */
.menu-slide-enter-active,
.menu-slide-leave-active {
  transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

.menu-slide-enter-from,
.menu-slide-leave-to {
  transform: translateX(-100%);
  opacity: 0;
}

.overlay-fade-enter-active,
.overlay-fade-leave-active {
  transition: opacity 0.5s ease;
}

.overlay-fade-enter-from,
.overlay-fade-leave-to {
  opacity: 0;
}

.material-icons {
  font-size: 24px;
  display: block;
}

.theme-toggle-container {
    justify-content: space-between !important;
}

.toggle-label {
    flex: 1;
}

.theme-switch-btn {
    transform: scale(0.9);
    margin-right: -2px;
}

@media (max-width: 600px) {
    .app-header {
        height: 72px;
        padding: 0 16px;
    }
    .header-title {
        font-size: 20px;
    }
}

/* Custom Menu Button */
.menu-btn-custom {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  border: none;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  font-size: 14px;
  font-family: 'Inter', Verdana, sans-serif;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  color: var(--text-primary);
  cursor: pointer;
  letter-spacing: 1px;
  box-shadow: none;
}

.menu-btn-custom:hover {
  transform: scale(1.1);
  color: var(--active-color);
}

.menu-btn-custom .icon {
    display: flex;
    align-items: center;
}

/* Logout Custom Button */
.logout-container {
  padding: 24px 32px;
  border-top: 1px solid var(--border-color);
  margin-top: 8px;
}

.logout-btn-custom {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 45px;
  height: 45px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition-duration: .3s;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.2);
  background-color: var(--active-color);
  background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
}

.sign {
  width: 45px;
  transition-duration: .3s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sign svg {
  width: 18px;
}

.sign svg path {
  fill: white;
}

.btn-text {
  position: absolute;
  right: 0%;
  width: 0%;
  opacity: 0;
  color: white;
  font-size: 0.75rem;
  font-weight: 800;
  transition-duration: .3s;
  white-space: nowrap;
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

.logout-btn-custom:hover {
  width: 180px;
  border-radius: 40px;
  transition-duration: .3s;
}

.logout-btn-custom:hover .sign {
  width: 30%;
  transition-duration: .3s;
  padding-left: 15px;
}

.logout-btn-custom:hover .btn-text {
  opacity: 1;
  width: 70%;
  transition-duration: .3s;
  padding-right: 15px;
}

.logout-btn-custom:active {
  transform: translate(2px ,2px);
}
</style>
