<script setup lang="ts">
/**
 * NexusLayout — V2 Dashboard Shell
 * Flex row, 100vh, overflow hidden.
 * Sidebar (248px) + Main (flex:1, flex-column)
 * Mobile: Sidebar als Overlay mit Hamburger-Toggle
 */
import { computed, ref } from 'vue'
import { RouterView, useRoute } from 'vue-router'
import { useTaskBoard } from '../api/taskBoard'
import { useOpenClawOverviewQuery } from '../api/openclawRuntime'
import GalaxyBackground from '../components/background/GalaxyBackground.vue'
import AppSidebar from '../components/layout/AppSidebar.vue'
import Topbar from '../components/layout/Topbar.vue'
import { useAuthStore } from '../stores/auth'
import { useMissionControlUiStore } from '../stores/missionControlUi'

const taskBoard = useTaskBoard(50)
const overviewQuery = useOpenClawOverviewQuery()
const missionControlUi = useMissionControlUiStore()
const auth = useAuthStore()
const route = useRoute()

const activeView = computed(() => String(route.name ?? 'Dashboard'))
const queuedTasks = computed(() =>
  taskBoard.board.value.offen.length
  + taskBoard.board.value.inProgress.length
  + taskBoard.board.value.review.length
  + taskBoard.board.value.blocked.length,
)

/* ── Mobile Sidebar State ───────────────────────── */
const mobileMenuOpen = ref(false)

function closeMobileMenu() {
  mobileMenuOpen.value = false
}

</script>

<template>
  <div class="nexus-layout">
    <GalaxyBackground />
    <AppSidebar
      :active-view="activeView"
      :mobile-nav-open="mobileMenuOpen"
      :queued-tasks="queuedTasks"
      :incidents="0"
      @navigate="closeMobileMenu"
    />

    <!-- Mobile Backdrop -->
    <button
      v-if="mobileMenuOpen"
      type="button"
      class="mobile-backdrop"
      aria-label="Close navigation"
      @click="closeMobileMenu"
    ></button>

    <main class="nexus-main">
      <Topbar
        :connected="overviewQuery.data.value?.connection.connected ?? false"
        :status-label="overviewQuery.data.value?.connection.gatewayVersion ? `OpenClaw ${overviewQuery.data.value.connection.gatewayVersion}` : 'OpenClaw status pending'"
        :iris-chat-open="missionControlUi.irisOpen"
        :command-open="missionControlUi.commandOpen"
        :can-open-iris="auth.isOwner"
        @toggle-sidebar="mobileMenuOpen = !mobileMenuOpen"
        @open-command="missionControlUi.openCommand"
        @open-iris="missionControlUi.openIris"
      />
      <div class="nexus-content">
        <RouterView />
      </div>
    </main>
  </div>
</template>

<style scoped>
.nexus-layout {
  display: flex;
  flex-direction: row;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

.nexus-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
  z-index: 1;
}

.nexus-content {
  flex: 1;
  overflow: hidden;
  min-height: 0;
}

.mobile-backdrop {
  display: none;
}

@media (max-width: 900px) {
  .nexus-main {
    width: 100%;
  }

  .mobile-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 99;
    padding: 0;
    border: 0;
    background: color-mix(in srgb, var(--space-0) 50%, transparent);
  }
}
</style>
