<template>
  <aside class="sidebar" :class="{ collapsed, 'mobile-open': mobileOpen }">
    <div class="sidebar-header">
      <LayoutDashboard :size="24" class="sidebar-logo-icon" />
      <span v-show="!collapsed" class="sidebar-title">Space Booking</span>
    </div>

    <nav class="sidebar-nav">
      <div class="nav-section">
        <span v-show="!collapsed" class="nav-section-label">Main</span>
        <router-link
          v-for="item in mainNav"
          :key="item.to"
          :to="item.to"
          class="nav-link"
          :class="{ active: isActive(item.to) }"
          @click="closeMobile"
        >
          <component :is="item.icon" :size="20" class="nav-icon" />
          <span v-show="!collapsed" class="nav-label">{{ item.label }}</span>
        </router-link>
      </div>

      <div v-if="authStore.isAdmin" class="nav-section">
        <span v-show="!collapsed" class="nav-section-label">Admin</span>
        <router-link
          v-for="item in adminNav"
          :key="item.to"
          :to="item.to"
          class="nav-link"
          :class="{ active: isActive(item.to) }"
          @click="closeMobile"
        >
          <component :is="item.icon" :size="20" class="nav-icon" />
          <span v-show="!collapsed" class="nav-label">{{ item.label }}</span>
        </router-link>
      </div>
    </nav>

    <div class="sidebar-footer">
      <div v-show="!collapsed" class="user-info">
        <div class="user-avatar">
          {{ authStore.user?.email?.charAt(0).toUpperCase() }}
        </div>
        <span class="user-email">{{ authStore.user?.email }}</span>
      </div>

      <div class="footer-actions">
        <button class="footer-btn" @click="toggleTheme" :title="themeTitle">
          <Sun v-if="resolvedTheme === 'light'" :size="18" />
          <Moon v-else-if="resolvedTheme === 'dark'" :size="18" />
        </button>

        <button class="footer-btn collapse-toggle desktop-only" @click="toggle" :title="collapsed ? 'Expand sidebar' : 'Collapse sidebar'">
          <ChevronRight v-if="collapsed" :size="18" />
          <ChevronLeft v-else :size="18" />
        </button>

        <button class="footer-btn logout-btn" @click="handleLogout" title="Logout">
          <LogOut :size="18" />
        </button>
      </div>
    </div>
  </aside>

  <div v-if="mobileOpen" class="sidebar-overlay" @click="closeMobile" />
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useSidebar } from '@/composables/useSidebar'
import { useTheme } from '@/composables/useTheme'
import {
  LayoutDashboard,
  Building2,
  CalendarDays,
  User,
  Settings2,
  Users,
  ClipboardCheck,
  Sliders,
  BarChart3,
  ScrollText,
  Sun,
  Moon,
  ChevronLeft,
  ChevronRight,
  LogOut
} from 'lucide-vue-next'

const authStore = useAuthStore()
const route = useRoute()
const router = useRouter()
const { collapsed, mobileOpen, toggle, closeMobile } = useSidebar()
const { theme, resolvedTheme, toggleTheme } = useTheme()

const themeTitle = computed(() => {
  if (theme.value === 'light') return 'Switch to dark mode'
  if (theme.value === 'dark') return 'Switch to auto mode'
  return 'Switch to light mode'
})

const mainNav = [
  { to: '/dashboard', icon: LayoutDashboard, label: 'Dashboard' },
  { to: '/spaces', icon: Building2, label: 'Spaces' },
  { to: '/my-bookings', icon: CalendarDays, label: 'My Bookings' },
  { to: '/profile', icon: User, label: 'Profile' },
]

const adminNav = [
  { to: '/admin', icon: Settings2, label: 'Spaces Admin' },
  { to: '/users', icon: Users, label: 'Users' },
  { to: '/admin/pending', icon: ClipboardCheck, label: 'Pending' },
  { to: '/admin/settings', icon: Sliders, label: 'Settings' },
  { to: '/admin/reports', icon: BarChart3, label: 'Reports' },
  { to: '/admin/audit-log', icon: ScrollText, label: 'Audit Log' },
]

const isActive = (path: string) => {
  if (path === '/dashboard') return route.path === '/dashboard'
  return route.path.startsWith(path)
}

const handleLogout = () => {
  authStore.logout()
  router.push('/login')
}
</script>

<style scoped>
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: width var(--transition-normal), transform var(--transition-normal);
  overflow: hidden;
}

.sidebar.collapsed {
  width: var(--sidebar-collapsed-width);
}

/* Header */
.sidebar-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1.25rem 1.25rem 1rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  min-height: 60px;
}

.sidebar-logo-icon {
  color: var(--color-accent);
  flex-shrink: 0;
}

.sidebar-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--sidebar-text-active);
  white-space: nowrap;
  overflow: hidden;
}

/* Navigation */
.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 0.75rem 0;
}

.nav-section {
  margin-bottom: 0.5rem;
}

.nav-section-label {
  display: block;
  padding: 0.5rem 1.25rem 0.25rem;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--sidebar-text);
  opacity: 0.6;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1.25rem;
  color: var(--sidebar-text);
  text-decoration: none;
  transition: all var(--transition-fast);
  border-left: 3px solid transparent;
  white-space: nowrap;
}

.nav-link:hover {
  background: var(--sidebar-hover-bg);
  color: var(--sidebar-text-active);
}

.nav-link.active {
  color: var(--sidebar-text-active);
  background: var(--sidebar-hover-bg);
  border-left-color: var(--color-accent);
}

.nav-icon {
  flex-shrink: 0;
}

.nav-label {
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Footer */
.sidebar-footer {
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  padding: 0.75rem 1rem;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0.25rem;
  margin-bottom: 0.5rem;
  overflow: hidden;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--color-accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 600;
  flex-shrink: 0;
}

.user-email {
  font-size: 0.8rem;
  color: var(--sidebar-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.footer-actions {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.footer-btn {
  background: transparent;
  border: none;
  color: var(--sidebar-text);
  cursor: pointer;
  padding: 0.5rem;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition-fast);
}

.footer-btn:hover {
  background: var(--sidebar-hover-bg);
  color: var(--sidebar-text-active);
}

.logout-btn:hover {
  color: var(--color-danger);
}

/* Sidebar overlay for mobile */
.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 99;
}

/* Mobile styles */
@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
    width: var(--sidebar-width);
  }

  .sidebar.mobile-open {
    transform: translateX(0);
  }

  .sidebar.collapsed {
    width: var(--sidebar-width);
  }

  .desktop-only {
    display: none;
  }
}
</style>
