<script setup lang="ts">
import { RouterView, RouterLink, useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { useServerStore } from '@/stores/server'
import {
  LayoutDashboard,
  Server,
  Terminal,
  Users,
  Puzzle,
  RefreshCw,
  Map,
  MessageSquare,
  BarChart3,
  Bell,
  UserPlus,
  ShoppingBag,
  Package,
  Settings,
  LogOut,
  Shield,
  Key,
  CreditCard,
  Network,
} from 'lucide-vue-next'

const route = useRoute()
const router = useRouter()
const auth = useAuthStore()
const server = useServerStore()

const navItems = [
  { name: 'Dashboard', path: '/', icon: LayoutDashboard },
  { name: 'Server', path: '/server', icon: Server },
  { name: 'Console', path: '/console', icon: Terminal },
  { name: 'Players', path: '/players', icon: Users },
  { name: 'Plugins', path: '/plugins', icon: Puzzle },
  { name: 'Auto-Wiper', path: '/wipes', icon: RefreshCw },
  { name: 'Maps', path: '/maps', icon: Map },
  { name: 'Chat Log', path: '/chat', icon: MessageSquare },
  { name: 'Analytics', path: '/analytics', icon: BarChart3 },
  { name: 'Notifications', path: '/notifications', icon: Bell },
  { name: 'Team', path: '/team', icon: UserPlus },
  { name: 'Store', path: '/store/manage', icon: ShoppingBag },
  { name: 'Modules', path: '/modules', icon: Package },
  { name: 'Settings', path: '/settings', icon: Settings },
]

const adminNavItems = [
  { name: 'Admin Home', path: '/admin', icon: Shield },
  { name: 'Licenses', path: '/admin/licenses', icon: Key },
  { name: 'Subscriptions', path: '/admin/subscriptions', icon: CreditCard },
  { name: 'Users', path: '/admin/users', icon: Users },
  { name: 'Server Fleet', path: '/admin/servers', icon: Network },
]

function isActive(path: string): boolean {
  if (path === '/' || path === '/admin') return route.path === path
  return route.path.startsWith(path)
}

function handleLogout() {
  auth.logout()
  router.push('/login')
}
</script>

<template>
  <div class="flex h-screen bg-neutral-950">
    <!-- Sidebar -->
    <aside class="w-64 bg-neutral-900 border-r border-neutral-800 flex flex-col">
      <!-- Logo -->
      <div class="p-4 border-b border-neutral-800">
        <div class="flex items-center gap-3">
          <img src="/logo.png" alt="Corrosion" class="h-8 w-8" />
          <div>
            <h1 class="text-sm font-bold text-oxide-500 tracking-wider">CORROSION</h1>
            <p class="text-xs text-neutral-500">{{ auth.license?.server_name || 'Server Management' }}</p>
          </div>
        </div>
      </div>

      <!-- Server Status Indicator -->
      <div class="px-4 py-3 border-b border-neutral-800">
        <div class="flex items-center gap-2">
          <div
            class="w-2 h-2 rounded-full"
            :class="{
              'bg-green-500': server.connection?.connection_status === 'connected',
              'bg-yellow-500': server.connection?.connection_status === 'degraded',
              'bg-red-500': server.connection?.connection_status === 'offline' || !server.connection,
            }"
          />
          <span class="text-sm text-neutral-400">
            {{ server.stats?.player_count ?? 0 }}/{{ server.stats?.max_players ?? 0 }} players
          </span>
        </div>
      </div>

      <!-- Navigation -->
      <nav class="flex-1 overflow-y-auto py-2">
        <RouterLink
          v-for="item in navItems"
          :key="item.path"
          :to="item.path"
          class="flex items-center gap-3 px-4 py-2 mx-2 rounded-lg text-sm transition-colors"
          :class="isActive(item.path)
            ? 'bg-oxide-500/10 text-oxide-400'
            : 'text-neutral-400 hover:bg-neutral-800 hover:text-neutral-200'"
        >
          <component :is="item.icon" class="w-4 h-4" />
          {{ item.name }}
        </RouterLink>

        <!-- Platform Admin Section (super-admin only) -->
        <template v-if="auth.isSuperAdmin">
          <div class="mt-4 mb-2 px-4">
            <div class="flex items-center gap-2">
              <div class="flex-1 border-t border-neutral-700" />
              <span class="text-[10px] font-semibold uppercase tracking-widest text-oxide-500">Platform</span>
              <div class="flex-1 border-t border-neutral-700" />
            </div>
          </div>
          <RouterLink
            v-for="item in adminNavItems"
            :key="item.path"
            :to="item.path"
            class="flex items-center gap-3 px-4 py-2 mx-2 rounded-lg text-sm transition-colors"
            :class="isActive(item.path)
              ? 'bg-oxide-500/10 text-oxide-400'
              : 'text-neutral-400 hover:bg-neutral-800 hover:text-neutral-200'"
          >
            <component :is="item.icon" class="w-4 h-4" />
            {{ item.name }}
          </RouterLink>
        </template>
      </nav>

      <!-- User -->
      <div class="p-4 border-t border-neutral-800">
        <div class="flex items-center justify-between">
          <div>
            <p class="text-sm text-neutral-300">{{ auth.user?.username }}</p>
            <p class="text-xs text-neutral-500">{{ auth.user?.email }}</p>
          </div>
          <button
            @click="handleLogout"
            class="text-neutral-500 hover:text-oxide-400 transition-colors"
          >
            <LogOut class="w-4 h-4" />
          </button>
        </div>
      </div>
    </aside>

    <!-- Main Content -->
    <main class="flex-1 overflow-y-auto">
      <RouterView />
    </main>
  </div>
</template>
