<template>
  <nav class="bottom-nav" aria-label="Main navigation">
    <RouterLink
      v-for="tab in tabs"
      :key="tab.name"
      :to="tab.to"
      :class="['bottom-nav__tab', { 'bottom-nav__tab--active': tab.isActive(route) }]"
      :aria-label="tab.label"
      :aria-current="tab.isActive(route) ? 'page' : undefined"
    >
      <span class="bottom-nav__icon-wrap" aria-hidden="true">
        <span class="bottom-nav__icon" v-html="tab.icon" />
        <span v-if="tab.name === 'library' && imagesStore.pendingCount > 0" class="bottom-nav__badge">
          {{ imagesStore.pendingCount > 9 ? '9+' : imagesStore.pendingCount }}
        </span>
      </span>
      <span class="bottom-nav__label">{{ tab.label }}</span>
    </RouterLink>
  </nav>
</template>

<script setup lang="ts">
import { useRoute, type RouteLocationNormalizedLoaded } from 'vue-router'
import { useImagesStore } from '@/stores/images'

const route       = useRoute()
const imagesStore = useImagesStore()

interface Tab {
  name: string
  label: string
  to: string
  icon: string
  isActive: (r: RouteLocationNormalizedLoaded) => boolean
}

const tabs: Tab[] = [
  {
    name: 'home',
    label: 'Home',
    to: '/',
    icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><polyline points="9,22 9,12 15,12 15,22"/></svg>',
    isActive: r => r.path === '/',
  },
  {
    name: 'library',
    label: 'Library',
    to: '/library',
    icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/></svg>',
    isActive: r => r.path.startsWith('/library'),
  },
  {
    name: 'settings',
    label: 'Settings',
    to: '/settings',
    icon: '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 1 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 1 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 1 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 1 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z"/></svg>',
    isActive: r => r.path.startsWith('/settings'),
  },
]
</script>

<style scoped lang="scss">
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  display: flex;
  z-index: 50;
  padding-bottom: env(safe-area-inset-bottom);

  @media (min-width: 960px) {
    display: none;
  }

  &__tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    height: 64px;
    color: var(--color-text-muted);
    text-decoration: none;
    min-height: var(--touch-min);
    transition: color var(--duration-fast);

    &--active {
      color: var(--color-primary);
    }
  }

  &__icon-wrap {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
  }

  &__icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
  }

  &__badge {
    position: absolute;
    top: -4px;
    right: -6px;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    background: var(--color-primary);
    color: var(--color-primary-fg);
    border-radius: 999px;
    font-size: 10px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
  }

  &__label {
    font-size: var(--text-xs);
    font-weight: 600;
  }
}
</style>
