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

  let { data } = $props()

  const currentLang = $derived(locale.current)

  function playWhenReady(video) {
    if (!video) return

    const attempt = () => {
      video.muted = true
      video.playsInline = true
      video.play().catch(() => {
        // Autoplay bloqué : on attend une interaction
        const onInteraction = () => {
          video.play().catch(() => {})
          document.removeEventListener('click', onInteraction)
          document.removeEventListener('touchstart', onInteraction)
        }
        document.addEventListener('click', onInteraction)
        document.addEventListener('touchstart', onInteraction)
      })
    }

    if (video.readyState >= 3) {
      attempt()
    } else {
      video.addEventListener('canplay', attempt, { once: true })
    }
  }

  onMount(() => {
    playWhenReady(document.getElementById('home-video'))
    playWhenReady(document.getElementById('home-video-mobile'))
  })

  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
        playsinline
        loop
        preload="auto"
        id="home-video"
        class="home-video home-video-desktop"
        poster={data.backgroundVideoPoster ?? undefined}
      >
        <source src={data.backgroundVideo ?? '/assets/video/Website_version.mp4'} type="video/mp4" />
      </video>
      <video
        muted
        playsinline
        loop
        preload="auto"
        id="home-video-mobile"
        class="home-video home-video-mobile"
        poster={data.backgroundVideoMobilePoster ?? undefined}
      >
        <source src={data.backgroundVideoMobile ?? '/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 (min-width: 701px) and (max-width: 912px) {
    .home-subtitle {
      font-size: var(--font-size-subtitle-tablet);
      width: 70%;
    }
  }
</style>
