<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" />
      
      <Transition name="menu-slide">
        <div v-if="showMenu" class="menu-dropdown sibu-sidebar">
          <div class="sibu-header-plain">
            <div class="header-main-info">
              <div class="brand-tag-simple">SIBU</div>
              <div class="user-info-clean">
                <div class="user-avatar-circle">
                  <span class="material-icons">{{ authStore.isAuthenticated ? 'account_circle' : 'fingerprint' }}</span>
                  <div v-if="authStore.isAuthenticated" class="status-dot"></div>
                </div>
                <div class="user-text-meta">
                  <span class="status-msg">{{ authStore.isAuthenticated ? 'USUARIO ACTIVO' : 'MODO INVITADO' }}</span>
                  <span class="user-name-text">{{ authStore.isAuthenticated ? authStore.userName : 'Invitado' }}</span>
                </div>
              </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 admin-link" @click="navigateTo('/admin')">
                <span class="material-icons">shield_person</span>
                <span class="link-text">Panel de Control</span>
              </div>
              <div v-if="authStore.isDriver" class="sidebar-link driver-link" @click="navigateTo('/driver')">
                <span class="material-icons">minor_crash</span>
                <span class="link-text">Servicio Conductor</span>
              </div>
              <div v-if="authStore.isPromoter" class="sidebar-link promoter-link" @click="navigateTo('/promoter')">
                <span class="material-icons">store</span>
                <span class="link-text">Central Promotores</span>
              </div>
            </div>

            <div class="sidebar-group">
              <div class="group-label">MENU</div>
              <div class="sidebar-link" @click="navigateTo('/profile')">
                <span class="material-icons">account_circle</span>
                <span class="link-text">Mi Perfil</span>
              </div>
              <div class="sidebar-link" @click="navigateTo('/favorites')">
                <span class="material-icons">star</span>
                <span class="link-text">Favoritos</span>
              </div>
            </div>

            <div class="sidebar-group">
              <div class="group-label">CONFIGURACIÓN</div>
              <div class="sidebar-link" @click="toggleLanguage">
                <span class="material-icons">translate</span>
                <span class="link-text">{{ locale === 'es' ? 'Idioma: English' : 'Idioma: Español' }}</span>
              </div>
              <div class="sidebar-link theme-toggle-row">
                <span class="material-icons">palette</span>
                <span class="link-text">Modo Oscuro</span>
                <ThemeToggle class="sidebar-theme-switch" />
              </div>
            </div>

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

          <div class="sidebar-footer-plain">
            <div v-if="!authStore.isAuthenticated" class="footer-btn login" @click="navigateTo('/login')">
                <span class="material-icons">power</span> INICIAR SESIÓN
            </div>
            <div v-else class="footer-btn logout" @click="handleLogout">
                <span class="material-icons">power_settings_new</span> CERRAR SESIÓN
            </div>
            <div class="sibu-version-simple">SIBU 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: 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;
}

/* 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 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-header-plain {
    padding: 40px 24px 20px;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
}

.brand-tag-simple {
    font-size: 12px;
    font-weight: 900;
    color: var(--active-color);
    letter-spacing: 2px;
    margin-bottom: 12px;
}

.user-info-clean {
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-avatar-circle {
    position: relative;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--hover-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid var(--border-color);
}

.user-avatar-circle .material-icons {
    font-size: 28px;
    color: var(--text-secondary);
}

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

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

.status-msg {
    font-size: 9px;
    font-weight: 700;
    color: var(--text-secondary);
    letter-spacing: 0.5px;
}

.user-name-text {
    font-size: 16px;
    font-weight: 800;
    color: var(--text-primary);
}

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

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

.group-label {
    font-size: 10px;
    font-weight: 800;
    color: var(--text-secondary);
    padding: 0 12px 6px;
    letter-spacing: 1px;
    opacity: 0.7;
}

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

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

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

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

.link-text {
    font-size: 14px;
    font-weight: 600;
}

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

.sidebar-theme-switch {
    transform: scale(0.8);
    margin-right: -5px;
}

.report-link {
    color: #ef4444;
}

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

.footer-btn {
    padding: 12px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    font-weight: 800;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.3s;
}

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

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

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

.sibu-version-simple {
    margin-top: 12px;
    text-align: center;
    font-size: 9px;
    color: var(--text-secondary);
    opacity: 0.5;
}

.menu-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0,0,0,0.4);
    z-index: 9999;
}

/* Transitions */
.menu-slide-enter-active,
.menu-slide-leave-active {
  transition: transform 0.3s ease-out;
}

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

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

.menu-btn-custom:hover {
    background: var(--hover-bg);
}
</style>
