<script>
  import { onMount } from 'svelte'
  import { locale } from '@state/locale.svelte'
  import { navigateTo } from '@router'

  let { data } = $props()

  const currentLang = $derived(locale.current)

  onMount(() => {
    const playVideo = async (videoId) => {
      const video = document.getElementById(videoId)
      if (video) {
        try {
          video.muted = true
          video.playsInline = true
          const playPromise = video.play()
          if (playPromise !== undefined) {
            await playPromise
          }
        } catch (error) {
          console.log(`Autoplay failed for ${videoId}:`, error)
          const playOnInteraction = () => {
            video.play().catch(e => console.log('Fallback play failed:', e))
            document.removeEventListener('click', playOnInteraction)
            document.removeEventListener('touchstart', playOnInteraction)
          }
          document.addEventListener('click', playOnInteraction)
          document.addEventListener('touchstart', playOnInteraction)
        }
      }
    }

    const timer = setTimeout(() => {
      playVideo('home-video')
      playVideo('home-video-mobile')
    }, 100)

    return () => clearTimeout(timer)
  })

  function handleExplore() {
    navigateTo('/' + data.hero.ctaPath)
  }
</script>

<div class="home golden-grid" data-anchor="HOME">
  <div class="home-bg">
    <figure class="home-figure">
      <video
        muted
        autoplay
        playsinline
        loop
        controls={false}
        preload="metadata"
        id="home-video"
        class="home-video home-video-desktop"
      >
        <source src="/assets/video/Website_version.mp4" type="video/mp4" />
      </video>
      <video
        muted
        autoplay
        playsinline
        loop
        controls={false}
        preload="metadata"
        id="home-video-mobile"
        class="home-video home-video-mobile"
      >
        <source src="/assets/video/mobile_version_texte_fixe.mp4" type="video/mp4" />
      </video>
    </figure>
  </div>

  <div class="vertical-line-start"></div>
  <div class="vertical-line-center"></div>
  <div class="vertical-line-end"></div>

  <div class="home-text">
    <h2 class="font-face-danzza-light home-subtitle">
      {data.hero.subtitle}
    </h2>
    <div
      class="clickable button home-cta"
      onclick={handleExplore}
      onkeypress={(e) => e.key === 'Enter' && handleExplore()}
      role="button"
      tabindex="0"
    >
      <div class="earth-icon clickable-filter-black"></div>
      <p class="home-cta-text clickable">
        {data.hero.ctaText}
      </p>
    </div>
  </div>
</div>

<style>
  .home {
    background-color: rgba(0, 0, 0, 0);
  }

  /* Full-grid video background — replaces div[grid-area] + .olly */
  .home-bg {
    grid-area: 1/1 / span 20 / span 20;
    filter: saturate(140%);
    position: relative;
    overflow: hidden;
  }

  .home-figure {
    position: absolute;
    inset: 0;
    margin: 0;
    padding: 0;
  }

  .home-video {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .home-video-desktop {
    display: block !important;
  }

  .home-video-mobile {
    display: none !important;
  }

  /* Text overlay */
  .home-text {
    z-index: var(--z-content);
    grid-area: 9/1 / span 6 / span 20;
    width: 100%;
    justify-self: center;
    margin-top: 6vmax;
  }

  .home-subtitle {
    font-size: var(--font-size-subtitle);
    color: white;
    width: 30%;
    margin: auto;
  }

  /* CTA button */
  .home-cta {
    margin: auto;
    margin-top: 40px;
  }

  .home-cta-text {
    font-family: Terminal;
    font-size: 1.2em;
  }

  @media screen and (max-width: 1000px) {
    .home-video-desktop {
      display: none !important;
    }
    .home-video-mobile {
      display: block !important;
    }

    .home-subtitle {
      font-size: var(--font-size-subtitle-mobile);
      width: 80%;
      line-height: 1.4;
    }
  }

  @media (--tablet-only) {
    .home-subtitle {
      font-size: var(--font-size-subtitle-tablet);
      width: 70%;
    }
  }
</style>
