<template>
  <v-app-bar class="main-nav" aria-label="Main Navigation" role="navigation">
    <v-container :class="containerClasses" :style="{ maxWidth: maxContainerWidth }" fluid>
      <v-row align="center" no-gutters>
        <v-app-bar-title class="app-title">
          <button
            aria-label="Go to home page"
            class="home-link"
            type="button"
            @click="goHome"
            @keydown.enter="goHome"
            @keydown.space.prevent="goHome">
            {{ appTitle }}
          </button>
          <a
            :href="repoUrl"
            aria-label="ALHP Git Repository (opens in new tab)"
            class="ms-3 repo-link"
            rel="noopener noreferrer"
            target="_blank">
            <v-icon icon="mdi-git" size="24" />
          </a>
        </v-app-bar-title>

        <v-spacer></v-spacer>

        <!-- Desktop/Tablet: Full stats -->
        <build-stats v-if="isDesktop || isTablet" :show-lto="isDesktop" />

        <!-- Mobile: Compact stats -->
        <div v-else-if="!statsStore.state.loading && !statsStore.state.error" class="mobile-stats">
          <span class="mobile-stat mobile-stat--success">
            {{ statsStore.state.stats?.latest || 0 }}
          </span>
          <span class="mobile-stat mobile-stat--error">
            {{ statsStore.state.stats?.failed || 0 }}
          </span>
        </div>

        <v-btn
          :aria-label="isDark ? 'Switch to light mode' : 'Switch to dark mode'"
          class="theme-toggle ms-4"
          icon
          size="small"
          variant="text"
          @click="toggleTheme">
          <v-icon :icon="isDark ? 'mdi-weather-sunny' : 'mdi-weather-night'" />
          <v-tooltip activator="parent" location="bottom">
            {{ isDark ? 'Light mode' : 'Dark mode' }}
          </v-tooltip>
        </v-btn>
      </v-row>
    </v-container>
  </v-app-bar>
</template>

<script lang="ts" setup>
import BuildStats from '@/components/MainNav/BuildStats.vue'
import { useDisplay } from 'vuetify'
import { computed } from 'vue'
import { usePackagesStore, useStatsStore } from '@/stores'
import { useTheme } from '@/composables/useTheme'

const { mobile, width } = useDisplay()
const packagesStore = usePackagesStore()
const statsStore = useStatsStore()
const { isDark, toggleTheme } = useTheme()

const goHome = () => {
  packagesStore.resetFilters()
}
const isTablet = computed(() => mobile && width.value >= 650 && width.value < 960)
const isDesktop = computed(() => !mobile.value && !isTablet.value)

const appTitle = 'ALHP Status'
const repoUrl = 'https://somegit.dev/ALHP/ALHP.GO'
const maxContainerWidth = '1440px'

const containerClasses = computed(() => ({
  'ms-3': width.value < 1440,
  'mx-auto': width.value >= 1440
}))
</script>

<style scoped>
.main-nav {
  background: var(--color-bg-secondary) !important;
  border-bottom: 1px solid var(--color-border);
}

.app-title {
  align-self: center;
  font-size: var(--font-size-xl);
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.home-link {
  color: var(--color-text-primary);
  text-decoration: none;
  font-weight: var(--font-weight-semibold);
  transition: color var(--transition-fast), transform var(--transition-fast);
  background: none;
  border: none;
  padding: 0;
  font-size: inherit;
  font-family: inherit;
  cursor: pointer;
}

.home-link:hover {
  color: var(--color-brand-primary);
  transform: translateY(-1px);
}

.home-link:focus-visible {
  outline: 2px solid var(--color-brand-primary);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.repo-link {
  color: var(--color-text-secondary);
  text-decoration: none;
  transition: color var(--transition-fast), transform var(--transition-fast);
  display: inline-flex;
  align-items: center;
}

.repo-link:hover {
  color: var(--color-brand-accent);
  transform: scale(1.1);
}

.repo-link:focus-visible {
  outline: 2px solid var(--color-brand-primary);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.theme-toggle {
  color: var(--color-text-secondary) !important;
  transition: color var(--transition-fast), transform var(--transition-fast) !important;
}

.theme-toggle:hover {
  color: var(--color-text-primary) !important;
  transform: rotate(15deg);
}

/* Mobile stats display */
.mobile-stats {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.mobile-stat {
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  font-family: var(--font-family-mono);
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  background: var(--color-bg-tertiary);
}

.mobile-stat--success {
  color: var(--color-status-success);
}

.mobile-stat--error {
  color: var(--color-status-error);
}

@media (max-width: 600px) {
  .app-title {
    font-size: var(--font-size-lg);
  }
}
</style>
