<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">
          <div class="menu-header" v-if="authStore.isAuthenticated">
              <span class="user-name">{{ authStore.userName }}</span>
          </div>
          
          <!-- Unified Menu Items -->
          <div class="menu-item" @click="navigateTo('/profile')">
              <span class="material-icons">person</span> {{ t('navigation.profile') }}
          </div>
          
          <template v-if="authStore.isAdmin">
            <div class="menu-item" @click="navigateTo('/admin')">
                <span class="material-icons">admin_panel_settings</span> {{ t('navigation.admin') }}
            </div>
          </template>
          
          <template v-if="authStore.isPromoter">
            <div class="menu-item" @click="navigateTo('/promoter')">
                <span class="material-icons">store</span> Panel Promotor
            </div>
          </template>

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

          <div class="menu-item" @click="navigateTo('/favorites')">
              <span class="material-icons">favorite</span> {{ t('navigation.favorites') }}
          </div>

          <div class="menu-divider"></div>

          <div class="menu-divider"></div>

          <!-- Bottom Menu Group -->
          <div class="menu-bottom-group">
            <div class="menu-item language-toggle" @click="toggleLanguage">
                <span class="material-icons">translate</span> 
                {{ locale === 'es' ? 'English (EN)' : 'Español (ES)' }}
            </div>

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

            <div class="menu-item report-menu-item" @click="openReportModal">
                <span class="material-icons">report_problem</span> Enviar Reporte
            </div>

            <div v-if="!authStore.isAuthenticated" class="menu-item login-item" @click="navigateTo('/login')">
                <span class="material-icons">account_circle</span> Iniciar Sesión
            </div>
            <div v-else class="logout-container" @click="handleLogout">
              <button class="logout-btn-custom">
                <div class="sign">
                  <svg viewBox="0 0 512 512"><path d="M377.9 105.9L500.7 228.7c7.2 7.2 11.3 17.1 11.3 27.3s-4.1 20.1-11.3 27.3L377.9 406.1c-6.4 6.4-15 9.9-24 9.9c-18.7 0-33.9-15.2-33.9-33.9l0-62.1-128 0c-17.7 0-32-14.3-32-32l0-64c0-17.7 14.3-32 32-32l128 0 0-62.1c0-18.7 15.2-33.9 33.9-33.9c9 0 17.6 3.6 24 9.9zM160 96L96 96c-17.7 0-32 14.3-32 32l0 256c0 17.7 14.3 32 32 32l64 0c17.7 0 32 14.3 32 32s-14.3 32-32 32l-64 0c-53 0-96-43-96-96L0 128C0 75 43 32 96 32l64 0c17.7 0 32 14.3 32 32s-14.3 32-32 32z"></path></svg>
                </div>
                <div class="btn-text">Cerrar Sesión</div>
              </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: sticky;
  top: 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 {
    position: fixed;
    top: 0;
    left: 0;
    width: 300px;
    height: 100vh;
    background: var(--header-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-right: 1px solid var(--border-color);
    padding: 32px 0;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    box-shadow: 20px 0 50px rgba(0,0,0,0.5);
}

.menu-header {
    padding: 0 32px 32px;
    border-bottom: 1px solid var(--border-color);
    margin-bottom: 24px;
}

.user-name {
    font-size: 1.25rem;
    font-weight: 900;
    color: var(--active-color);
    display: block;
}

.menu-item {
    padding: 16px 32px;
    display: flex;
    align-items: center;
    gap: 16px;
    cursor: pointer;
    color: var(--text-primary);
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    font-weight: 600;
    font-size: 1.05rem;
}

.menu-item:hover {
    background-color: var(--hover-bg);
    padding-left: 48px;
    color: var(--active-color);
}

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

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

.menu-divider {
    height: 1px;
    background: var(--border-color);
    margin: 24px 32px;
}

.menu-bottom-group {
    margin-top: 24px;
    display: flex;
    flex-direction: column;
}

.report-menu-item {
    color: #fbbf24;
}

.report-menu-item:hover {
    background-color: rgba(251, 191, 36, 0.1);
    color: #f59e0b;
}

.report-menu-item .material-icons {
    color: #fbbf24;
}

.login-item {
    color: #4ade80;
    border-top: 1px solid var(--border-color);
    padding: 20px 32px calc(20px + var(--safe-area-bottom));
    background: rgba(74, 222, 128, 0.03);
}

.login-item:hover {
    background: rgba(74, 222, 128, 0.1);
    color: #22c55e;
    padding-left: 40px;
}

.login-item .material-icons {
    color: #4ade80;
}

.login-item:hover .material-icons {
    color: #22c55e;
    transform: scale(1.1);
}

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