<template>
  <div class="page-layout">
    <aside class="sidebar" aria-label="Navigation principale">
      <div class="sidebar-header">
        <div class="logo-container">
          <img
              :src="logoSrc"
              alt="Logo Malio"
              class="logo"
          >
        </div>
        <div class="brand-copy">
          <p class="brand-title">Supervisor</p>
        </div>
        <div class="sidebar-divider"/>
      </div>
      <div class="sidebar-content">
        <slot name="sidebar"/>
        <nav class="sidebar-nav" aria-label="Sections">
          <p class="nav-label">Navigation</p>
          <NuxtLink
              v-for="item in navItems"
              :key="`desktop-${item.to}`"
              v-slot="{ href, navigate, isExactActive }"
              :to="item.to"
              custom
          >
            <a
                :href="href"
                class="nav-link"
                :class="{ 'nav-link-active': isExactActive }"
                :aria-current="isExactActive ? 'page' : undefined"
                @click="navigate"
            >
              <span class="nav-link-main">
                <span class="nav-icon">
                  <IconifyIcon :icon="item.icon" class="text-lg"/>
                </span>
                <span>
                  <span class="nav-title">{{ item.label }}</span>
                  <span class="nav-caption">{{ item.caption }}</span>
                </span>
              </span>
              <span class="nav-pill">{{ item.short }}</span>
            </a>
          </NuxtLink>
        </nav>
      </div>
      <div class="sidebar-footer">
        <div class="sidebar-divider"/>
        <div class="status-card">
          <p class="status-label">Environnement</p>
          <p class="status-value">Production</p>
          <p class="status-description">
            Acces rapide au monitoring, aux sauvegardes et aux cartes systeme.
          </p>
        </div>
        <div class="footer-row">
          <p class="font-mono text-[10px] tracking-widest uppercase text-white/40">
            Supervisor {{ appVersion }}
          </p>
        </div>
      </div>
    </aside>

    <button class="mobile-menu-button" type="button" @click="isMenuOpen = true">
      <IconifyIcon icon="mdi:menu" class="text-2xl"/>
    </button>

    <div v-if="isMenuOpen" class="mobile-menu-backdrop" @click="isMenuOpen = false"/>

    <aside v-if="isMenuOpen" class="mobile-sidebar" aria-label="Navigation mobile">
      <div class="sidebar-header">
        <div class="logo-container">
          <img
              :src="logoSrc"
              alt="Logo Malio"
              class="logo"
          >
        </div>
        <div class="brand-copy">
          <p class="brand-kicker">Control Center</p>
          <p class="brand-title">Supervisor</p>
          <p class="brand-description">
            Tableau de bord interne pour le monitoring et les sauvegardes.
          </p>
        </div>
        <div class="sidebar-divider"/>
      </div>
      <div class="sidebar-content">
        <slot name="sidebar"/>
        <nav class="sidebar-nav" aria-label="Sections mobiles">
          <p class="nav-label">Navigation</p>
          <NuxtLink
              v-for="item in navItems"
              :key="`mobile-${item.to}`"
              v-slot="{ href, navigate, isExactActive }"
              :to="item.to"
              custom
          >
            <a
                :href="href"
                class="nav-link"
                :class="{ 'nav-link-active': isExactActive }"
                :aria-current="isExactActive ? 'page' : undefined"
                @click="navigate(); isMenuOpen = false"
            >
              <span class="nav-link-main">
                <span class="nav-icon">
                  <IconifyIcon :icon="item.icon" class="text-lg"/>
                </span>
                <span>
                  <span class="nav-title">{{ item.label }}</span>
                  <span class="nav-caption">{{ item.caption }}</span>
                </span>
              </span>
              <span class="nav-pill">{{ item.short }}</span>
            </a>
          </NuxtLink>
        </nav>
      </div>
      <div class="sidebar-footer">
        <div class="sidebar-divider"/>
        <div class="status-card">
          <p class="status-label">Environnement</p>
          <p class="status-value">Production</p>
          <p class="status-description">
            Navigation rapide vers les vues principales de supervision.
          </p>
        </div>
        <div class="footer-row">
          <p class="font-mono text-[10px] tracking-widest uppercase text-white/40">
            Supervisor {{ appVersion }}
          </p>
          <button class="close-button" type="button" @click="isMenuOpen = false">
            <IconifyIcon icon="mdi:close" class="text-xl"/>
          </button>
        </div>
      </div>
    </aside>

    <main class="content bg-noise">
      <slot/>
    </main>
  </div>
</template>

<script setup lang="ts">
import {ref} from "vue"
import {Icon as IconifyIcon} from "@iconify/vue"
import logoSrc from '~/assets/LOGO_CARRE_BLANC.png'

const {
  public: {appVersion}
} = useRuntimeConfig()
const isMenuOpen = ref(false)
const navItems = [
  {
    to: "/",
    label: "Monitoring",
    caption: "Etat global et disponibilite",
    short: "MON",
    icon: "mdi:chart-box-outline"
  },
  {
    to: "/backup",
    label: "Backup",
    caption: "Scripts et fichiers archives",
    short: "BKP",
    icon: "mdi:database-arrow-up-outline"
  }
]
</script>

<style scoped>
.page-layout {
  display: grid;
  grid-template-columns: 260px 1fr;
  min-height: 100vh;
}

.sidebar,
.mobile-sidebar {
  background: linear-gradient(
      180deg,
      rgb(var(--m-sidebar-from)) 0%,
      rgb(var(--m-sidebar-to)) 100%
  );
  color: white;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--m-sidebar-border);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  transition: background 0.4s ease, border-color 0.4s ease;
}

.sidebar-header {
  padding: 1.5rem 2rem 0;
  flex-shrink: 0;
}

.logo-container {
  display: flex;
  justify-content: center;
  padding: 0.5rem 0;
}

.brand-copy {
  padding: 0.5rem 0 0.75rem;
  text-align: center;
}

.brand-kicker {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: rgb(var(--m-accent));
}

.brand-title {
  margin: 0.45rem 0 0;
  font-family: var(--font-display);
  font-size: 1.45rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.sidebar .brand-title {
  margin-top: 0;
}

.brand-description {
  margin: 0.55rem 0 0;
  color: rgb(255 255 255 / 0.58);
  line-height: 1.6;
  font-size: 0.92rem;
}

.logo {
  height: 100px;
  width: 100px;
  object-fit: contain;
  filter: drop-shadow(0 0 20px rgba(80, 140, 255, 0.15));
  transition: filter 0.3s ease;
}

.logo:hover {
  filter: drop-shadow(0 0 28px rgba(80, 140, 255, 0.3));
}

.sidebar-content {
  flex: 1;
  padding: 0.75rem 1rem 1rem;
}

.sidebar-footer {
  flex-shrink: 0;
  padding-bottom: 0.5rem;
}

.sidebar-divider {
  height: 1px;
  margin: 0.75rem 1.5rem;
  background: linear-gradient(
      90deg,
      transparent,
      var(--m-sidebar-divider),
      transparent
  );
}

.footer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 1.5rem 0.75rem;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
}

.nav-label {
  margin: 0;
  padding: 0 0.75rem 0.25rem;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: rgb(255 255 255 / 0.38);
}

.nav-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 0.9rem;
  border-radius: 14px;
  border: 1px solid transparent;
  color: white;
  text-decoration: none;
  transition: background-color 0.2s ease,
  border-color 0.2s ease,
  transform 0.2s ease,
  box-shadow 0.2s ease;
}

.nav-link:hover {
  background: rgb(255 255 255 / 0.06);
  border-color: rgb(var(--m-accent) / 0.14);
  transform: translateY(-1px);
}

.nav-link:focus-visible {
  outline: 2px solid rgb(var(--m-accent));
  outline-offset: 2px;
  background: rgb(255 255 255 / 0.08);
}

.nav-link-active {
  background: linear-gradient(135deg, rgb(var(--m-accent) / 0.16), rgb(255 255 255 / 0.04));
  border-color: rgb(var(--m-accent) / 0.24);
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.04);
}

.nav-link-active .nav-icon {
  background: rgb(var(--m-accent) / 0.18);
  color: white;
}

.nav-link-active .nav-pill {
  background: rgb(var(--m-accent) / 0.18);
  color: white;
}

.nav-link-main {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  min-width: 0;
}

.nav-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 12px;
  background: rgb(255 255 255 / 0.06);
  color: rgb(var(--m-accent));
  flex-shrink: 0;
}

.nav-title,
.nav-caption {
  display: block;
}

.nav-title {
  font-size: 0.96rem;
  font-weight: 600;
  color: white;
}

.nav-caption {
  margin-top: 0.2rem;
  font-size: 0.78rem;
  line-height: 1.45;
  color: rgb(255 255 255 / 0.5);
}

.nav-pill {
  flex-shrink: 0;
  padding: 0.28rem 0.45rem;
  border-radius: 999px;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  color: rgb(255 255 255 / 0.62);
  background: rgb(255 255 255 / 0.06);
}

.status-card {
  margin: 0 1rem;
  padding: 1rem;
  border-radius: 16px;
  border: 1px solid rgb(var(--m-accent) / 0.14);
  background: radial-gradient(circle at top right, rgb(var(--m-accent) / 0.14), transparent 30%),
  rgb(255 255 255 / 0.04);
}

.status-label {
  margin: 0;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: rgb(255 255 255 / 0.42);
}

.status-value {
  margin: 0.5rem 0 0;
  font-size: 1rem;
  font-weight: 700;
  color: white;
}

.status-description {
  margin: 0.45rem 0 0;
  font-size: 0.82rem;
  line-height: 1.55;
  color: rgb(255 255 255 / 0.54);
}

.content {
  background: rgb(var(--m-bg));
  background-image:
    linear-gradient(180deg, rgb(255 255 255 / 0.01), transparent 18%),
    radial-gradient(circle at top right, rgb(var(--m-accent) / 0.08), transparent 20%);
  overflow-y: auto;
  min-height: 100vh;
  transition: background-color 0.4s ease;
}

.mobile-menu-button,
.mobile-menu-backdrop,
.mobile-sidebar {
  display: none;
}

@media (max-width: 820px) {
  .page-layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    display: none;
  }

  .mobile-menu-button {
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 40;
    display: flex;
    height: 44px;
    width: 44px;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    border: 1px solid rgb(var(--m-accent) / 0.18);
    background: rgb(var(--m-secondary));
    color: rgb(var(--m-text));
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.24);
  }

  .mobile-menu-backdrop {
    position: fixed;
    inset: 0;
    z-index: 45;
    display: block;
    background: rgba(3, 8, 20, 0.55);
    backdrop-filter: blur(3px);
  }

  .mobile-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 50;
    display: flex;
    height: 100vh;
    width: min(84vw, 320px);
    box-shadow: 0 18px 60px rgba(0, 0, 0, 0.38);
  }

  .sidebar-content {
    padding-right: 0.9rem;
    padding-left: 0.9rem;
  }

  .close-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 32px;
    width: 32px;
    border-radius: 8px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.06);
    color: white;
  }

  .nav-link {
    padding: 0.8rem 0.85rem;
  }
}
</style>
