<script setup lang="ts">
import { computed } from 'vue'

interface Props {
  isOpen: boolean
}

defineProps<Props>()
defineEmits<{
  toggle: []
}>()

const menuItems = [
  {
    icon: '⚙️',
    label: 'Model Operations',
    path: '/model-ops/shadow-run-jobs',
    children: [
      { label: 'Shadow Run Jobs', path: '/model-ops/shadow-run-jobs' },
      { label: 'Models Master', path: '/model-ops/models-master' },
    ],
  },
  {
    icon: '📋',
    label: 'Governance',
    path: '/governance/approvals',
    children: [
      { label: 'Approval Queue', path: '/governance/approvals' },
    ],
  },
  {
    icon: '📊',
    label: 'Analytics',
    path: '/analytics',
    children: [
      { label: 'Dashboard', path: '/analytics/dashboard' },
      { label: 'Reports', path: '/analytics/reports' },
    ],
  },
]

const isCurrentRoute = (path: string) => {
  return window.location.pathname.includes(path)
}
</script>

<template>
  <aside class="sidebar" :class="{ 'is-open': isOpen }">
    <!-- Logo -->
    <div class="sidebar-header">
      <div class="logo">
        <span class="logo-icon">🚀</span>
        <span v-if="isOpen" class="logo-text">K-ArtSell</span>
      </div>
    </div>

    <!-- Navigation -->
    <nav class="sidebar-nav">
      <div v-for="(item, idx) in menuItems" :key="idx" class="nav-section">
        <a
          :href="item.path"
          class="nav-item nav-parent"
          :class="{ 'is-active': isCurrentRoute(item.path) }"
        >
          <span class="nav-icon">{{ item.icon }}</span>
          <span v-if="isOpen" class="nav-label">{{ item.label }}</span>
        </a>

        <!-- Sub-items -->
        <div v-if="isOpen && item.children" class="nav-children">
          <a
            v-for="(child, cidx) in item.children"
            :key="cidx"
            :href="child.path"
            class="nav-item nav-child"
            :class="{ 'is-active': isCurrentRoute(child.path) }"
          >
            <span class="nav-label">{{ child.label }}</span>
          </a>
        </div>
      </div>
    </nav>

    <!-- Footer Info -->
    <div v-if="isOpen" class="sidebar-footer">
      <div class="sidebar-version">v1.0.0</div>
    </div>
  </aside>
</template>

<style scoped>
.sidebar {
  width: var(--sidebar-width-base);
  background-color: var(--color-background-secondary);
  border-right: var(--border-width-1) solid var(--color-border-primary);
  display: flex;
  flex-direction: column;
  transition: width var(--transition-base);
  overflow-x: hidden;
}

.sidebar.is-open {
  width: var(--sidebar-width-base);
}

.sidebar:not(.is-open) {
  width: var(--sidebar-width-compact);
}

.sidebar-header {
  padding: var(--spacing-4);
  border-bottom: var(--border-width-1) solid var(--color-border-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.logo {
  display: flex;
  align-items: center;
  gap: var(--spacing-2);
  font-weight: var(--font-weight-bold);
  color: var(--color-primary-600);
}

.logo-icon {
  font-size: 24px;
  line-height: 1;
}

.logo-text {
  font-size: var(--font-size-lg);
  white-space: nowrap;
}

.sidebar-nav {
  flex: 1;
  padding: var(--spacing-2);
  overflow-y: auto;
}

.nav-section {
  margin-bottom: var(--spacing-3);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--spacing-2);
  padding: var(--spacing-2) var(--spacing-3);
  border-radius: var(--border-radius-base);
  color: var(--color-text-secondary);
  text-decoration: none;
  transition: all var(--transition-fast);
  cursor: pointer;
  font-size: var(--font-size-sm);
  white-space: nowrap;
}

.nav-item:hover {
  background-color: var(--color-background-hover);
  color: var(--color-text-primary);
}

.nav-item.is-active {
  background-color: var(--color-primary-50);
  color: var(--color-primary-700);
  font-weight: var(--font-weight-semibold);
}

.nav-icon {
  font-size: 18px;
  flex-shrink: 0;
  line-height: 1;
}

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

.nav-parent {
  font-weight: var(--font-weight-medium);
}

.nav-children {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-1);
  margin-top: var(--spacing-1);
  padding-left: var(--spacing-2);
  border-left: var(--border-width-1) solid var(--color-border-secondary);
}

.nav-child {
  padding: var(--spacing-1) var(--spacing-2);
  padding-left: var(--spacing-3);
  font-size: var(--font-size-xs);
}

.nav-child:hover {
  background-color: transparent;
  padding-left: var(--spacing-4);
}

.sidebar-footer {
  padding: var(--spacing-3);
  border-top: var(--border-width-1) solid var(--color-border-secondary);
  font-size: var(--font-size-xs);
  color: var(--color-text-tertiary);
  text-align: center;
}

.sidebar-version {
  font-weight: var(--font-weight-medium);
}

/* Scrollbar styling */
.sidebar-nav::-webkit-scrollbar {
  width: 6px;
}

.sidebar-nav::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar-nav::-webkit-scrollbar-thumb {
  background: var(--color-border-secondary);
  border-radius: 3px;
}

.sidebar-nav::-webkit-scrollbar-thumb:hover {
  background: var(--color-border-primary);
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    height: 100vh;
    width: var(--sidebar-width-base);
    z-index: var(--z-index-fixed);
    box-shadow: var(--shadow-lg);
  }

  .sidebar:not(.is-open) {
    transform: translateX(-100%);
  }
}
</style>
