<template>
  <div class="layout">
    <header class="site-header">
      <div class="container header-inner">
        <NuxtLink to="/" class="site-logo">
          <img v-if="logoUrl" :src="logoUrl" alt="BasicStack" class="logo-image" />
        </NuxtLink>

        <button class="nav-toggle" aria-label="Toggle menu" @click="menuOpen = !menuOpen">
          <span />
          <span />
          <span />
        </button>

        <nav class="site-nav" :class="{ open: menuOpen }">
          <template v-for="item in topLevelNav" :key="item.id">
            <NuxtLink
              v-if="resolveNavHref(item)"
              :to="resolveNavHref(item)!"
              class="nav-link"
              :target="item.type === 'external' ? '_blank' : undefined"
              @click="menuOpen = false"
            >
              {{ item.label }}
            </NuxtLink>
          </template>
        </nav>
      </div>
    </header>

    <main>
      <slot />
    </main>

    <footer class="site-footer">
      <div class="container footer-inner">
        <p class="footer-brand">BasicStack</p>
        <p class="footer-tagline">Production-ready open source infrastructure</p>
        <p class="footer-copy">&copy; {{ new Date().getFullYear() }} BasicStack</p>
      </div>
    </footer>
  </div>
</template>

<script setup lang="ts">
const { getNavigation, getSiteSettings, getAssetUrl } = useDirectus()
const menuOpen = ref(false)

const { data: navItems } = await useAsyncData('navigation', () => getNavigation())
const { data: siteSettings } = await useAsyncData('site-settings', () => getSiteSettings())

const topLevelNav = computed(() =>
  (navItems.value ?? []).filter(item => !item.parent_id)
)

const logoUrl = computed(() =>
  siteSettings.value?.logo ? getAssetUrl(siteSettings.value.logo) : null
)

function resolveNavHref(item: { type: string; external_url: string | null; custom_route: string | null; page_id: number | null }) {
  if (item.type === 'external') return item.external_url
  if (item.type === 'custom') return item.custom_route
  if (item.type === 'page' && item.page_id) return `/${item.page_id}`
  return null
}
</script>

<style scoped>
.layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--color-bg);
  border-bottom: 1px solid var(--color-border);
  height: var(--header-height);
}

.header-inner {
  display: flex;
  align-items: center;
  height: 100%;
  gap: var(--space-xl);
}

.site-logo {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  text-decoration: none;
  margin-right: auto;
}

.logo-image {
  height: 48px;
  width: auto;
}

.site-nav {
  display: flex;
  align-items: center;
  gap: var(--space-lg);
}

.nav-link {
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--color-text-muted);
  text-decoration: none;
  transition: color 0.2s;
  padding: var(--space-xs) 0;
}

.nav-link:hover,
.nav-link.router-link-active {
  color: var(--color-primary);
  text-decoration: none;
}

.nav-toggle {
  display: none;
  flex-direction: column;
  gap: 5px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 4px;
}

.nav-toggle span {
  display: block;
  width: 24px;
  height: 2px;
  background: var(--color-text);
  border-radius: 2px;
  transition: transform 0.2s;
}

main {
  flex: 1;
}

.site-footer {
  background: var(--color-bg-secondary);
  border-top: 1px solid var(--color-border);
  padding: var(--space-2xl) 0;
  margin-top: auto;
}

.footer-inner {
  text-align: center;
}

.footer-brand {
  font-size: 1.125rem;
  font-weight: 700;
  margin: 0 0 var(--space-xs);
}

.footer-tagline {
  color: var(--color-text-muted);
  margin: 0 0 var(--space-md);
  font-size: 0.9375rem;
}

.footer-copy {
  color: var(--color-text-muted);
  font-size: 0.875rem;
  margin: 0;
}

@media (max-width: 768px) {
  .nav-toggle {
    display: flex;
  }

  .site-nav {
    display: none;
    position: absolute;
    top: var(--header-height);
    left: 0;
    right: 0;
    background: var(--color-bg);
    border-bottom: 1px solid var(--color-border);
    flex-direction: column;
    padding: var(--space-lg);
    gap: var(--space-md);
    align-items: flex-start;
  }

  .site-nav.open {
    display: flex;
  }
}
</style>
