<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">{{ t('menu.admin') }}</div>
      <div v-if="authStore.isDriver" class="driver-badge">{{ t('menu.driver') }}</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 sib-sidebar">
          <div class="sib-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">{{ t('menu.welcome') }}</span>
                <span class="user-name-highlight">{{ authStore.isAuthenticated ? authStore.userName : t('menu.guest') }}</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">{{ t('menu.management') }}</div>
              <div v-if="authStore.isAdmin" class="sidebar-link" @click="navigateTo('/admin')">
                <span class="material-icons">shield_person</span>
                <span class="link-text">{{ t('menu.adminPanel') }}</span>
              </div>
              <div v-if="authStore.isDriver && !authStore.isAdmin" class="sidebar-link" @click="navigateTo('/driver')">
                <span class="material-icons">minor_crash</span>
                <span class="link-text">{{ t('menu.driverPanel') }}</span>
              </div>
            </div>

            <div v-if="!authStore.isAdmin" class="sidebar-group">
              <div class="group-label">{{ t('menu.operations') }}</div>
              <div class="sidebar-link" @click="navigateTo('/favorites')">
                <span class="material-icons">favorite</span>
                <span class="link-text">{{ t('menu.favorites') }}</span>
              </div>
              <div class="sidebar-link" @click="toggleLanguage">
                <span class="material-icons">translate</span>
                <span class="link-text">{{ t('menu.translate') }}</span>
              </div>
            </div>

              <div class="sidebar-link theme-toggle-row" @click="themeStore.toggleDarkMode">
                <span class="material-icons">{{ themeStore.isDarkMode ? 'light_mode' : 'dark_mode' }}</span>
                <span class="link-text">{{ themeStore.isDarkMode ? t('menu.lightMode') : t('menu.darkMode') }}</span>
              </div>

            <div v-if="!authStore.isAdmin" class="sidebar-group">
              <div class="group-label">{{ t('menu.support') }}</div>
              <div class="sidebar-link report-link-solid" @click="openReportModal">
                <span class="material-icons">report_problem</span>
                <span class="link-text">{{ t('menu.sendReport') }}</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> {{ t('menu.login') }}
            </button>
            <button v-else class="session-btn logout-solid" @click="handleLogout">
                <span class="material-icons">logout</span> {{ t('menu.logout') }}
            </button>
            <div class="sib-tag-footer">SIB 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, defineAsyncComponent } from 'vue'
import { useAuthStore } from '@/stores/auth'
import { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'
import { useThemeStore } from '@/stores/theme'
const ReportModal = defineAsyncComponent(() => import('./ReportModal.vue'))

const { t, locale } = useI18n()
const authStore = useAuthStore()
const themeStore = useThemeStore()
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 = () => {
    showMenu.value = false
    authStore.logout()
}
</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;
}

.admin-badge, .driver-badge {
    padding: 4px 8px;
    border-radius: 6px;
    font-size: 10px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 1px;
    white-space: nowrap;
}

.admin-badge {
    background: rgba(254, 231, 21, 0.15);
    color: var(--active-color);
    border: 1px solid rgba(254, 231, 21, 0.3);
}

.driver-badge {
    background: rgba(74, 222, 128, 0.15);
    color: #4ade80;
    border: 1px solid rgba(74, 222, 128, 0.3);
}

@media (max-width: 600px) {
    .admin-badge, .driver-badge {
        display: none;
    }
}

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


/* SIB SIDEBAR STYLES (SOLID) */

/* SIB SIDEBAR OPTIMIZED */
.sib-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 280px;
    /* 100dvh = dynamic viewport height (excluye barra URL de Chrome Android) */
    /* Fallback para navegadores antiguos: -webkit-fill-available */
    height: 100vh;
    height: -webkit-fill-available;
    height: 100dvh;
    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);
    overflow: hidden;
}

.sib-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: 20px 20px;
    /* Añadir safe area para iPhone (notch inferior) y Android (barra de navegación) */
    padding-bottom: calc(20px + env(safe-area-inset-bottom, 0px));
    background: var(--bg-primary);
    border-top: 1px solid var(--border-color);
    margin-top: auto;
    flex-shrink: 0; /* Nunca se comprime, siempre visible */
}

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

.sib-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>
