<script>
  import { navigationStore } from '@stores/navigation'
  import { localeStore } from '@stores/locale'
  import { siteStore } from '@stores/site'
  import { navigateTo } from '@lib/router'

  $: isMenuOpen = $navigationStore.isMenuOpen
  $: currentLang = $localeStore.current
  $: site = $siteStore
  $: logo = site.logo
  $: siteTitle = site.title
  $: navigation = site.navigation || []
  $: languages = site.languages || []

  function handleNav(path) {
    navigateTo(path)
    navigationStore.closeMenu()
  }

  function handleLanguageChange(langCode) {
    // In a full implementation, this would switch to the translated URL
    localeStore.setLanguage(langCode)
  }
</script>

<header class="header" class:menu-open={isMenuOpen}>
  <div class="header__container">
    <!-- Logo -->
    <a href="/" on:click|preventDefault={() => handleNav('/')} class="header__logo">
      {#if logo}
        <img src={logo} alt={siteTitle} />
      {:else}
        <span class="header__logo-text">{siteTitle}</span>
      {/if}
    </a>

    <!-- Navigation -->
    <nav class="header__nav" class:open={isMenuOpen}>
      <ul class="header__nav-list">
        {#each navigation as item}
          {#if item.url}
            <li class="header__nav-item" class:active={item.isActive}>
              <a
                href={item.url}
                on:click|preventDefault={() => handleNav(item.url)}
              >
                {currentLang === 'en' ? item.label_en : item.label_fr}
              </a>
            </li>
          {/if}
        {/each}
      </ul>
    </nav>

    <!-- Language Switcher -->
    {#if languages.length > 1}
      <div class="header__lang">
        {#each languages as lang}
          <button
            class="header__lang-btn"
            class:active={lang.code === currentLang}
            on:click={() => handleLanguageChange(lang.code)}
          >
            {lang.code.toUpperCase()}
          </button>
        {/each}
      </div>
    {/if}

    <!-- Mobile Menu Toggle -->
    <button
      class="header__toggle"
      on:click={() => navigationStore.toggleMenu()}
      aria-label="Menu"
    >
      <span></span>
      <span></span>
      <span></span>
    </button>
  </div>
</header>

<style>
  .header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1000;
    background: rgba(0, 0, 0, 0.9);
    backdrop-filter: blur(10px);
  }

  .header__container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 1.5rem 2rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 2rem;
  }

  .header__logo {
    display: flex;
    align-items: center;
    text-decoration: none;
    z-index: 1001;
  }

  .header__logo img {
    height: 40px;
    width: auto;
  }

  .header__logo-text {
    font-size: 1.5rem;
    font-weight: 700;
    color: #fff;
  }

  .header__nav {
    flex: 1;
    display: flex;
    justify-content: center;
  }

  .header__nav-list {
    display: flex;
    gap: 2rem;
    list-style: none;
    margin: 0;
    padding: 0;
  }

  .header__nav-item a {
    color: #fff;
    text-decoration: none;
    font-weight: 500;
    text-transform: uppercase;
    font-size: 0.9rem;
    letter-spacing: 0.05em;
    transition: color 0.3s;
  }

  .header__nav-item a:hover,
  .header__nav-item.active a {
    color: #04fea0;
  }

  .header__lang {
    display: flex;
    gap: 0.5rem;
  }

  .header__lang-btn {
    background: none;
    border: 1px solid #fff;
    color: #fff;
    padding: 0.5rem 1rem;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 600;
    transition: all 0.3s;
  }

  .header__lang-btn:hover,
  .header__lang-btn.active {
    background: #04fea0;
    border-color: #04fea0;
    color: #000;
  }

  .header__toggle {
    display: none;
    flex-direction: column;
    gap: 4px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 0.5rem;
    z-index: 1001;
  }

  .header__toggle span {
    width: 25px;
    height: 2px;
    background: #fff;
    transition: all 0.3s;
  }

  @media (max-width: 768px) {
    .header__container {
      padding: 1rem;
    }

    .header__toggle {
      display: flex;
    }

    .header__nav {
      position: fixed;
      top: 0;
      right: 0;
      bottom: 0;
      width: 100%;
      max-width: 300px;
      background: rgba(0, 0, 0, 0.98);
      transform: translateX(100%);
      transition: transform 0.3s;
      padding: 5rem 2rem 2rem;
    }

    .header__nav.open {
      transform: translateX(0);
    }

    .header__nav-list {
      flex-direction: column;
      gap: 1.5rem;
    }

    .header__nav-item a {
      font-size: 1.2rem;
    }

    .header__lang {
      position: fixed;
      bottom: 2rem;
      right: 2rem;
    }

    .menu-open .header__toggle span:nth-child(1) {
      transform: rotate(45deg) translate(5px, 5px);
    }

    .menu-open .header__toggle span:nth-child(2) {
      opacity: 0;
    }

    .menu-open .header__toggle span:nth-child(3) {
      transform: rotate(-45deg) translate(7px, -6px);
    }
  }
</style>
