<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 Sidebar -->
      <Transition name="menu-slide">
        <div v-if="showMenu" class="menu-dropdown nexus-sidebar">
          <!-- NEXUS HEADER: Elite Identity -->
          <div class="nexus-header-premium">
            <div class="header-content-wrapper">
              <div class="brand-badge">SIBU NEXUS ELITE</div>
              <div class="user-profile-nexus">
                <div class="user-avatar-premium">
                  <span class="material-icons">{{ authStore.isAuthenticated ? 'account_circle' : 'fingerprint' }}</span>
                  <div v-if="authStore.isAuthenticated" class="online-pulse"></div>
                </div>
                <div class="user-meta">
                  <span class="user-status">{{ authStore.isAuthenticated ? 'SISTEMA ACTIVO' : 'ACCESO INVITADO' }}</span>
                  <span class="user-display-name">{{ authStore.isAuthenticated ? authStore.userName : 'NEXUS GUEST' }}</span>
                </div>
              </div>
            </div>
            <div class="header-bg-glow"></div>
          </div>

          <div class="menu-scroll-area">
            <!-- SECTION: GESTIÓN (Solo si es Admin/Conductor/Promotor) -->
            <div v-if="authStore.isAdmin || authStore.isDriver || authStore.isPromoter" class="nexus-section">
              <div class="section-label">GESTIÓN DE FLOTA</div>
              
              <div v-if="authStore.isAdmin" class="menu-item-premium admin-item" @click="navigateTo('/admin')">
                <div class="item-icon-wrapper"><span class="material-icons">shield_person</span></div>
                <span class="item-text">Panel Control Admin</span>
                <span class="material-icons chevron">chevron_right</span>
              </div>
              
              <div v-if="authStore.isDriver" class="menu-item-premium driver-item" @click="navigateTo('/driver')">
                <div class="item-icon-wrapper"><span class="material-icons">minor_crash</span></div>
                <span class="item-text">Servicio Conductor</span>
                <span class="material-icons chevron">chevron_right</span>
              </div>

              <div v-if="authStore.isPromoter" class="menu-item-premium promoter-item" @click="navigateTo('/promoter')">
                <div class="item-icon-wrapper"><span class="material-icons">store</span></div>
                <span class="item-text">Central Promotores</span>
                <span class="material-icons chevron">chevron_right</span>
              </div>
            </div>

            <!-- SECTION: NAVEGACIÓN -->
            <div class="nexus-section">
              <div class="section-label">NAVEGACIÓN</div>
              
              <div class="menu-item-premium" @click="navigateTo('/profile')">
                <div class="item-icon-wrapper"><span class="material-icons">account_circle</span></div>
                <span class="item-text">Mi Identidad Nexus</span>
                <span class="material-icons chevron">chevron_right</span>
              </div>
              
              <div class="menu-item-premium" @click="navigateTo('/favorites')">
                <div class="item-icon-wrapper"><span class="material-icons">star</span></div>
                <span class="item-text">Destinos Favoritos</span>
                <span class="material-icons chevron">chevron_right</span>
              </div>
            </div>

            <!-- SECTION: SISTEMA -->
            <div class="nexus-section">
              <div class="section-label">CONFIGURACIÓN SISTEMA</div>
              
              <div class="menu-item-premium" @click="toggleLanguage">
                <div class="item-icon-wrapper"><span class="material-icons">translate</span></div>
                <span class="item-text">{{ locale === 'es' ? 'Protocolo: English (EN)' : 'Protocolo: Español (ES)' }}</span>
              </div>

              <div class="menu-item-premium theme-toggle-item">
                <div class="item-icon-wrapper"><span class="material-icons">palette</span></div>
                <span class="item-text">Modo Visual Oscuro</span>
                <ThemeToggle class="theme-switch-btn" />
              </div>
            </div>

            <!-- SECTION: SOPORTE -->
            <div class="nexus-section">
              <div class="section-label">SOPORTE TÉCNICO</div>
              <div class="menu-item-premium report-item" @click="openReportModal">
                <div class="item-icon-wrapper"><span class="material-icons">report_problem</span></div>
                <span class="item-text">Reportar Incidencia</span>
              </div>
            </div>
          </div>

          <!-- FOOTER: Auth & Version -->
          <div class="nexus-sidebar-footer">
            <div v-if="!authStore.isAuthenticated" class="auth-btn-nexus login" @click="navigateTo('/login')">
                <span class="material-icons">power</span> INICIAR SISTEMA
            </div>
            <div v-else class="auth-btn-nexus logout" @click="handleLogout">
                <span class="material-icons">power_settings_new</span> CERRAR PROTOCOLO
            </div>
            <div class="nexus-version-tag">SIBU NEXUS CORE v1.0.4.STARK</div>
          </div>
        </div>
      </Transition>
    </div>
    
    <h1 class="header-title" @click="goToHome">{{ t('header.title') }}</h1>
    
    <div class="header-actions">
      <!-- Side Menu handles all actions for a 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')
}
</script>

<style scoped>
.app-header {
  background-color: var(--header-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  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.4s cubic-bezier(0.4, 0, 0.2, 1);
}

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

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

:global(.dark) .header-title {
  color: var(--active-color);
  text-shadow: 0 0 15px rgba(254, 231, 21, 0.3);
}

.header-title:hover {
  transform: scale(1.02);
  letter-spacing: 0.05em;
}

/* NEXUS SIDEBAR STYLES */
.nexus-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 300px;
    height: 100vh;
    background: var(--glass-bg);
    backdrop-filter: blur(30px) saturate(150%);
    -webkit-backdrop-filter: blur(30px) saturate(150%);
    border-right: 1px solid var(--glass-border);
    z-index: 10000;
    display: flex;
    flex-direction: column;
    box-shadow: 25px 0 60px rgba(0,0,0,0.5);
    overflow: hidden;
}

.nexus-header-premium {
    position: relative;
    padding: 50px 24px 30px;
    overflow: hidden;
}

.header-content-wrapper {
    position: relative;
    z-index: 2;
}

.brand-badge {
    display: inline-block;
    background: var(--active-color);
    color: #101820;
    font-size: 10px;
    font-weight: 900;
    padding: 4px 10px;
    border-radius: 6px;
    letter-spacing: 1px;
    margin-bottom: 16px;
    box-shadow: 0 4px 15px rgba(254, 231, 21, 0.4);
}

.user-profile-nexus {
    display: flex;
    align-items: center;
    gap: 16px;
}

.user-avatar-premium {
    position: relative;
    width: 56px;
    height: 56px;
    border-radius: 18px;
    background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--active-color);
    box-shadow: 0 0 20px rgba(254, 231, 21, 0.15);
}

.user-avatar-premium .material-icons {
    font-size: 32px;
    color: var(--active-color);
}

.online-pulse {
    position: absolute;
    bottom: -2px;
    right: -2px;
    width: 14px;
    height: 14px;
    background: #4ade80;
    border: 3px solid #0f172a;
    border-radius: 50%;
    box-shadow: 0 0 10px #4ade80;
    animation: pulse-green 2s infinite;
}

@keyframes pulse-green {
  0% { box-shadow: 0 0 0 0 rgba(74, 222, 128, 0.7); }
  70% { box-shadow: 0 0 0 10px rgba(74, 222, 128, 0); }
  100% { box-shadow: 0 0 0 0 rgba(74, 222, 128, 0); }
}

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

.user-status {
    font-size: 10px;
    font-weight: 700;
    color: var(--text-secondary);
    letter-spacing: 1.5px;
}

.user-display-name {
    font-size: 18px;
    font-weight: 800;
    color: var(--text-primary);
    text-transform: uppercase;
}

.header-bg-glow {
    position: absolute;
    top: -50px;
    right: -50px;
    width: 150px;
    height: 150px;
    background: radial-gradient(circle, rgba(254, 231, 21, 0.1) 0%, transparent 70%);
    z-index: 1;
}

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

.nexus-section {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.section-label {
    font-size: 11px;
    font-weight: 800;
    color: var(--text-secondary);
    padding: 0 12px 8px;
    letter-spacing: 2px;
    opacity: 0.6;
}

.menu-item-premium {
    padding: 12px 14px;
    border-radius: 14px;
    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-premium:hover {
    background: var(--hover-bg);
    border-color: var(--border-color);
    transform: translateX(6px);
}

.item-icon-wrapper {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.03);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.3s;
}

.menu-item-premium:hover .item-icon-wrapper {
    background: var(--active-bg);
    transform: scale(1.1);
}

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

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

.item-text {
    flex: 1;
    font-weight: 600;
    font-size: 14px;
}

.chevron {
    font-size: 18px;
    opacity: 0;
    transform: translateX(-10px);
    transition: all 0.3s;
}

.menu-item-premium:hover .chevron {
    opacity: 0.5;
    transform: translateX(0);
}

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

.report-item {
    background: rgba(239, 68, 68, 0.05);
    color: #f87171;
}

.nexus-sidebar-footer {
    padding: 24px;
    border-top: 1px solid var(--border-color);
    background: linear-gradient(to top, rgba(0,0,0,0.1), transparent);
}

.auth-btn-nexus {
    padding: 16px;
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    font-weight: 800;
    font-size: 13px;
    cursor: pointer;
    transition: all 0.4s;
    letter-spacing: 1px;
}

.auth-btn-nexus.login {
    background: var(--active-color);
    color: #101820;
    box-shadow: 0 10px 20px rgba(254, 231, 21, 0.2);
}

.auth-btn-nexus.logout {
    background: rgba(239, 68, 68, 0.1);
    color: #ef4444;
    border: 1px solid rgba(239, 68, 68, 0.2);
}

.auth-btn-nexus.logout:hover {
    background: #ef4444;
    color: white;
    transform: translateY(-2px);
    box-shadow: 0 10px 25px rgba(239, 68, 68, 0.3);
}

.nexus-version-tag {
    margin-top: 16px;
    text-align: center;
    font-size: 9px;
    font-weight: 700;
    color: var(--text-secondary);
    opacity: 0.5;
    letter-spacing: 1px;
}

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

/* Transitions */
.menu-slide-enter-active,
.menu-slide-leave-active {
  transition: all 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}

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

.menu-btn-custom {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  border: none;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
}

.menu-btn-custom:hover {
    background: var(--active-bg);
    color: var(--active-color);
    transform: rotate(90deg);
}
</style>
