<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" />
      
      <!-- Overlay para cerrar el menú al tocar fuera -->
      <Transition name="fade">
        <div v-if="showMenu" class="menu-overlay" @click="showMenu = false"></div>
      </Transition>

      <Transition name="menu-slide">
        <div v-if="showMenu" class="menu-dropdown sibu-sidebar">
          <div class="sibu-header-plain">
            <div class="user-profile-optimized">
              <div class="user-avatar-main">
                <span class="material-icons user-icon-accent">person</span>
                <div v-if="authStore.isAuthenticated" class="status-dot-active"></div>
              </div>
              <div class="user-info-text">
                <span class="welcome-label">HOLA,</span>
                <span class="user-name-highlight">{{ authStore.isAuthenticated ? authStore.userName : 'INVITADO' }}</span>
              </div>
            </div>
          </div>

          <div class="menu-scroll-area">
            <div v-if="authStore.isAdmin || authStore.isDriver || authStore.isPromoter" class="sidebar-group">
              <div class="group-label">GESTIÓN</div>
              <div v-if="authStore.isAdmin" class="sidebar-link" @click="navigateTo('/admin')">
                <span class="material-icons">shield_person</span>
                <span class="link-text">Panel Control</span>
              </div>
              <div v-if="authStore.isDriver" class="sidebar-link" @click="navigateTo('/driver')">
                <span class="material-icons">minor_crash</span>
                <span class="link-text">Taxi Panel</span>
              </div>
            </div>

            <div class="sidebar-group">
              <div class="group-label">OPERACIONES</div>
              <div class="sidebar-link" @click="navigateTo('/favorites')">
                <span class="material-icons">favorite</span>
                <span class="link-text">Favoritos</span>
              </div>
              <div class="sidebar-link" @click="toggleLanguage">
                <span class="material-icons">translate</span>
                <span class="link-text">{{ locale === 'es' ? 'English (EN)' : 'Español (ES)' }}</span>
              </div>
            </div>

            <div class="sidebar-group">
              <div class="group-label">APARIENCIA</div>
              <div class="sidebar-link theme-toggle-row">
                <span class="material-icons">dark_mode</span>
                <span class="link-text">Modo Visual</span>
                <ThemeToggle class="sidebar-theme-switch" />
              </div>
            </div>

            <div class="sidebar-group">
              <div class="group-label">SOPORTE</div>
              <div class="sidebar-link report-link-solid" @click="openReportModal">
                <span class="material-icons">report_problem</span>
                <span class="link-text">Enviar Reporte</span>
              </div>
            </div>
          </div>

          <div class="sidebar-footer-fixed">
            <button v-if="!authStore.isAuthenticated" class="session-btn login-solid" @click="navigateTo('/login')">
                <span class="material-icons">login</span> INICIAR SESIÓN
            </button>
            <button v-else class="session-btn logout-solid" @click="handleLogout">
                <span class="material-icons">logout</span> CERRAR SESIÓN
            </button>
            <div class="sibu-tag-footer">SIBU SYSTEM v1.2.0</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: var(--safe-area-top) 20px 0; /* Añadir espacio para el Notch */
  height: calc(64px + var(--safe-area-top));
  border-bottom: 1px solid var(--border-color);
  position: sticky; /* Cambiado de fixed a sticky para mejor integración */
  top: 0;
  left: 0;
  right: 0;
  z-index: 2000;
  width: 100%;
}

.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;
}

/* SIBU SIDEBAR STYLES (SOLID) */
.sibu-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 280px;
    height: 100vh;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    z-index: 10000;
    display: flex;
    flex-direction: column;
    box-shadow: 10px 0 30px rgba(0,0,0,0.2);
    overflow: hidden;
}

/* SIBU SIDEBAR OPTIMIZED */
.sibu-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 280px;
    height: 100vh;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    z-index: 10000;
    display: flex;
    flex-direction: column;
    box-shadow: 10px 0 40px rgba(0,0,0,0.3);
}

.sibu-header-plain {
    padding: 50px 24px 20px;
    background: var(--bg-primary);
    border-bottom: 2px solid var(--active-color);
}

.user-profile-optimized {
    display: flex;
    align-items: center;
    gap: 15px;
}

.user-avatar-main {
    position: relative;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: var(--active-color);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}

.user-icon-accent {
    font-size: 30px;
    color: #101820 !important; /* Always dark for contrast against yellow */
}

.status-dot-active {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 12px;
    height: 12px;
    background: #4ade80;
    border: 2px solid var(--bg-primary);
    border-radius: 50%;
}

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

.welcome-label {
    font-size: 10px;
    font-weight: 800;
    color: var(--text-secondary);
}

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

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

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

.group-label {
    font-size: 10px;
    font-weight: 900;
    color: var(--active-color);
    padding: 0 10px 8px;
    letter-spacing: 1.5px;
    text-transform: uppercase;
}

.sidebar-link {
    padding: 12px 15px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 15px;
    cursor: pointer;
    color: var(--text-primary);
    transition: background 0.2s;
}

.sidebar-link:hover {
    background: var(--hover-bg);
}

.sidebar-link .material-icons {
    font-size: 22px;
    color: var(--text-secondary);
}

.sidebar-link:hover .material-icons {
    color: var(--active-color);
}

.link-text {
    font-size: 15px;
    font-weight: 700;
}

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

.sidebar-theme-switch {
    transform: scale(0.9);
}

.report-link-solid {
    background: rgba(239, 68, 68, 0.1);
    color: #ef4444 !important;
    border-radius: 12px;
    padding: 12px 15px;
    display: flex;
    align-items: center;
    gap: 15px;
}

.sidebar-footer-fixed {
    padding: 25px 20px;
    background: var(--bg-primary);
    border-top: 1px solid var(--border-color);
    margin-top: auto;
}

.session-btn {
    width: 100%;
    padding: 15px;
    border-radius: 14px;
    border: none;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    font-weight: 900;
    font-size: 14px;
    cursor: pointer;
    transition: transform 0.2s, background 0.3s;
}

.session-btn.login-solid {
    background: var(--active-color);
    color: #101820;
}

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

.session-btn:active {
    transform: scale(0.98);
}

.sibu-tag-footer {
    margin-top: 15px;
    text-align: center;
    font-size: 10px;
    font-weight: 800;
    color: var(--text-secondary);
    opacity: 0.6;
}

/* Overlay & Transitions */
.menu-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0,0,0,0.5);
    z-index: 9999;
}

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

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

.menu-slide-enter-active,
.menu-slide-leave-active {
  transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

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

.menu-btn-custom {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: transparent;
  color: var(--text-primary);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>
