---
import ThemeToggle from './ThemeToggle.astro';
import LangToggle from './LangToggle.astro';
import { type Lang } from '@/i18n/utils';
import type { TranslationKey } from '@/i18n/ui';

interface Props {
  t: (key: TranslationKey) => string;
  lang: Lang;
  currentPath?: string;
}

const { t, lang, currentPath } = Astro.props;
const path = currentPath || Astro.url.pathname;

const navItems: { label: string; href: string; key: TranslationKey }[] = [
  { label: t('nav.home'), href: lang === 'en' ? '/en/' : '/', key: 'nav.home' },
  { label: t('nav.services'), href: lang === 'en' ? '/en/servicios/' : '/servicios/', key: 'nav.services' },
  { label: t('nav.plans'), href: lang === 'en' ? '/en/planes/' : '/planes/', key: 'nav.plans' },
  { label: t('nav.about'), href: lang === 'en' ? '/en/nosotros/' : '/nosotros/', key: 'nav.about' },
  { label: t('nav.tutorials'), href: lang === 'en' ? '/en/instructivos/' : '/instructivos/', key: 'nav.tutorials' },
  { label: t('nav.contact'), href: lang === 'en' ? '/en/contacto/' : '/contacto/', key: 'nav.contact' },
];

const whatsappUrl = 'https://wa.me/59899812487';
const isActive = (href: string) => {
  const normalized = path.replace(/\/$/, '') || '/';
  const target = href.replace(/\/$/, '') || '/';
  return normalized === target;
};
---
<header id="navbar" class="sticky top-0 z-40 backdrop-blur-md border-b transition-colors" style="background: rgba(250,246,238,0.85); border-color: var(--hds-line);">
  <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
    <div class="flex items-center justify-between h-16 md:h-20">
      <a href={lang === 'en' ? '/en/' : '/'} class="flex items-center gap-2.5 flex-shrink-0" aria-label="Hosting del Sur - Inicio">
        <img
          src="/logo/hds-mark.svg"
          alt=""
          width="32"
          height="32"
          class="h-8 w-8"
          aria-hidden="true"
        />
        <span class="text-base font-display tracking-tight" style="color: var(--hds-fg);">
          <span style="color: var(--color-hds-naranja);">hosting</span> del <span style="color: var(--color-hds-naranja);">sur</span>
        </span>
      </a>

      <nav class="hidden lg:flex items-center gap-7" aria-label="Principal">
        {navItems.map((item) => (
          <a
            href={item.href}
            class:list={[
              "text-sm font-medium transition-colors",
              isActive(item.href) ? "text-hds-naranja" : ""
            ]}
            style={isActive(item.href) ? 'color: var(--color-hds-naranja);' : 'color: var(--hds-fg-soft);'}
          >
            {item.label}
          </a>
        ))}
      </nav>

      <div class="flex items-center gap-2">
        <LangToggle lang={lang} />
        <ThemeToggle lang={lang} />
        <a
          href={whatsappUrl}
          target="_blank"
          rel="noopener noreferrer"
          class="hidden md:inline-flex btn-primary text-sm"
        >
          <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
            <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413Z"/>
          </svg>
          <span>{t('nav.cta')}</span>
        </a>

        <button
          type="button"
          class="lg:hidden p-2 -mr-2"
          aria-label="Abrir menú"
          aria-expanded="false"
          aria-controls="mobile-menu"
          id="mobile-menu-button"
          style="color: var(--hds-fg);"
        >
          <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
            <line x1="3" y1="6" x2="21" y2="6"/>
            <line x1="3" y1="12" x2="21" y2="12"/>
            <line x1="3" y1="18" x2="21" y2="18"/>
          </svg>
        </button>
      </div>
    </div>

    <div id="mobile-menu" class="lg:hidden hidden border-t py-4" style="border-color: var(--hds-line);">
      <nav class="flex flex-col gap-3" aria-label="Móvil">
        {navItems.map((item) => (
          <a
            href={item.href}
            class="text-base font-medium py-2"
            style={isActive(item.href) ? 'color: var(--color-hds-naranja);' : 'color: var(--hds-fg-soft);'}
          >
            {item.label}
          </a>
        ))}
        <a
          href={whatsappUrl}
          target="_blank"
          rel="noopener noreferrer"
          class="md:hidden inline-flex items-center justify-center gap-2 btn-primary text-sm mt-2"
        >
          <span>{t('nav.cta')}</span>
        </a>
      </nav>
    </div>
  </div>
</header>

<script is:inline>
  (function() {
    const btn = document.getElementById('mobile-menu-button');
    const menu = document.getElementById('mobile-menu');
    if (!btn || !menu) return;
    btn.addEventListener('click', () => {
      const isOpen = !menu.classList.contains('hidden');
      if (isOpen) {
        menu.classList.add('hidden');
        btn.setAttribute('aria-expanded', 'false');
      } else {
        menu.classList.remove('hidden');
        btn.setAttribute('aria-expanded', 'true');
      }
    });
  })();
</script>

<style>
  :global(:root.dark) #navbar {
    background: rgba(26,22,18,0.85) !important;
  }
</style>
