// ============================================================================
// MOVA RELOCATION — Composants partagés (icônes, primitives d'animation)
// ============================================================================

const { useState, useEffect, useRef, useCallback, useMemo } = React;

// ----------------------------------------------------------------------------
// Hook scroll position
// ----------------------------------------------------------------------------
function useScrollY(scrollRef) {
  const [y, setY] = useState(0);
  useEffect(() => {
    const el = scrollRef?.current || window;
    const get = () =>
    el === window ? window.scrollY : el.scrollTop;
    const onScroll = () => setY(get());
    onScroll();
    el.addEventListener("scroll", onScroll, { passive: true });
    return () => el.removeEventListener("scroll", onScroll);
  }, [scrollRef]);
  return y;
}

// ----------------------------------------------------------------------------
// Hook: reveal au scroll (IntersectionObserver, once)
// ----------------------------------------------------------------------------
function useInView(options = {}) {
  const ref = useRef(null);
  const [inView, setInView] = useState(false);
  useEffect(() => {
    if (!ref.current) return;
    const obs = new IntersectionObserver(
      ([entry]) => {
        if (entry.isIntersecting) {
          setInView(true);
          obs.disconnect();
        }
      },
      { threshold: 0.18, rootMargin: "0px 0px -10% 0px", ...options }
    );
    obs.observe(ref.current);
    return () => obs.disconnect();
  }, []);
  return [ref, inView];
}

// ----------------------------------------------------------------------------
// Reveal — fade + translateY au scroll
// ----------------------------------------------------------------------------
function Reveal({ children, delay = 0, y = 40, as = "div", className = "", style = {} }) {
  const [ref, inView] = useInView();
  const Tag = as;
  return (
    <Tag
      ref={ref}
      className={className}
      style={{
        opacity: inView ? 1 : 0,
        transform: `translateY(${inView ? 0 : y}px)`,
        transition: `opacity 900ms cubic-bezier(0.22,0.61,0.36,1) ${delay}ms, transform 900ms cubic-bezier(0.22,0.61,0.36,1) ${delay}ms`,
        ...style
      }}>
      
      {children}
    </Tag>);

}

// ----------------------------------------------------------------------------
// GoldRule — trait or qui se trace au scroll
// ----------------------------------------------------------------------------
function GoldRule({ width = 60, delay = 200, className = "", style = {} }) {
  const [ref, inView] = useInView();
  return (
    <span
      ref={ref}
      aria-hidden="true"
      className={className}
      style={{
        display: "inline-block",
        width,
        height: 0.5,
        background: "var(--mova-gold)",
        transformOrigin: "left center",
        transform: `scaleX(${inView ? 1 : 0})`,
        transition: `transform 1100ms cubic-bezier(0.22,0.61,0.36,1) ${delay}ms`,
        ...style
      }} />);


}

// ----------------------------------------------------------------------------
// Eyebrow — petite étiquette serif en majuscules + trait
// ----------------------------------------------------------------------------
function Eyebrow({ children, color = "var(--mova-gold)", center = false, withRule = true }) {
  return (
    <div
      style={{
        display: "flex",
        alignItems: "center",
        gap: 14,
        justifyContent: center ? "center" : "flex-start",
        marginBottom: 28
      }}>
      
      {withRule && center && <GoldRule width={36} />}
      <span
        style={{
          fontFamily: "var(--font-sans)",
          fontWeight: 300,
          fontSize: 11,
          letterSpacing: "0.42em",
          color,
          textTransform: "uppercase"
        }}>
        
        {children}
      </span>
      {withRule && <GoldRule width={36} delay={center ? 300 : 200} />}
    </div>);

}

// ----------------------------------------------------------------------------
// MovaWordmark — logo "MOVA" + "RELOCATION"
// ----------------------------------------------------------------------------
function MovaWordmark({ size = 22, light = false, stacked = false }) {
  return (
    <div
      style={{
        display: "inline-flex",
        flexDirection: stacked ? "column" : "row",
        alignItems: stacked ? "flex-start" : "baseline",
        gap: stacked ? 6 : 16,
        color: light ? "var(--mova-cream)" : "var(--mova-cream)"
      }}>
      
      <span
        style={{

          fontWeight: 200,
          fontSize: size,
          letterSpacing: "0.4em",
          lineHeight: 1, fontFamily: "\"Cormorant Garamond\""
        }}>
        
        MOVA
      </span>
      <span
        style={{
          fontFamily: "var(--font-sans)",
          fontWeight: 300,
          fontSize: Math.max(9, Math.round(size * 0.46)),
          letterSpacing: "0.32em",
          color: "var(--mova-gold)",
          lineHeight: 1
        }}>
        
        RELOCATION
      </span>
    </div>);

}

// ----------------------------------------------------------------------------
// Bouton primaire — fond off-white / texte noir, hover or
// ----------------------------------------------------------------------------
function PillButton({
  children,
  href,
  variant = "dark",
  size = "md",
  external = true,
  onClick
}) {
  const palette = {
    dark: {
      bg: "var(--mova-black)",
      fg: "var(--mova-cream)",
      hoverBg: "var(--mova-gold)",
      hoverFg: "var(--mova-black)",
      border: "var(--mova-black)"
    },
    cream: {
      bg: "var(--mova-cream)",
      fg: "var(--mova-black)",
      hoverBg: "var(--mova-gold)",
      hoverFg: "var(--mova-black)",
      border: "var(--mova-cream)"
    },
    ghost: {
      bg: "transparent",
      fg: "var(--mova-cream)",
      hoverBg: "var(--mova-cream)",
      hoverFg: "var(--mova-black)",
      border: "var(--mova-cream)"
    }
  }[variant];
  const padding = size === "lg" ? "22px 56px" : size === "md" ? "16px 36px" : "12px 28px";
  const fontSize = size === "lg" ? 12 : 11;
  const [hover, setHover] = useState(false);
  const style = {
    display: "inline-flex",
    alignItems: "center",
    justifyContent: "center",
    padding,
    fontFamily: "var(--font-sans)",
    fontWeight: 400,
    fontSize,
    letterSpacing: "0.32em",
    textTransform: "uppercase",
    textDecoration: "none",
    border: `0.5px solid ${palette.border}`,
    background: hover ? palette.hoverBg : palette.bg,
    color: hover ? palette.hoverFg : palette.fg,
    cursor: "pointer",
    transition:
    "background 420ms cubic-bezier(0.22,0.61,0.36,1), color 420ms cubic-bezier(0.22,0.61,0.36,1), border-color 420ms",
    whiteSpace: "nowrap"
  };
  const props = {
    style,
    onMouseEnter: () => setHover(true),
    onMouseLeave: () => setHover(false),
    onClick
  };
  if (href) {
    return (
      <a
        href={href}
        target={external ? "_blank" : undefined}
        rel={external ? "noopener noreferrer" : undefined}
        {...props}>
        
        {children}
      </a>);

  }
  return <button {...props}>{children}</button>;
}

// ----------------------------------------------------------------------------
// Icônes line-art SVG — stroke or 0.5px
// ----------------------------------------------------------------------------
const ICON_STROKE = "var(--mova-gold)";

function Icon({ name, size = 56 }) {
  const common = {
    width: size,
    height: size,
    viewBox: "0 0 64 64",
    fill: "none",
    stroke: ICON_STROKE,
    strokeWidth: 0.8,
    strokeLinecap: "round",
    strokeLinejoin: "round"
  };
  switch (name) {
    case "house":
      return (
        <svg {...common}>
          <path d="M10 30 L32 12 L54 30" />
          <path d="M16 28 V52 H48 V28" />
          <path d="M28 52 V38 H36 V52" />
          <path d="M40 22 V14 H46 V26" />
        </svg>);

    case "box":
      return (
        <svg {...common}>
          <path d="M10 20 L32 10 L54 20 L32 30 Z" />
          <path d="M10 20 V46 L32 56 V30" />
          <path d="M54 20 V46 L32 56" />
          <path d="M21 15 L43 25" />
        </svg>);

    case "book":
      return (
        <svg {...common}>
          <path d="M32 16 C28 13 22 11 12 11 V49 C22 49 28 51 32 54" />
          <path d="M32 16 C36 13 42 11 52 11 V49 C42 49 36 51 32 54" />
          <path d="M32 16 V54" />
        </svg>);

    case "car":
      return (
        <svg {...common}>
          <path d="M10 40 L14 28 C15 25 17 24 20 24 H44 C47 24 49 25 50 28 L54 40" />
          <path d="M8 40 H56 V48 H50 V44 H14 V48 H8 Z" />
          <circle cx="18" cy="48" r="3.5" />
          <circle cx="46" cy="48" r="3.5" />
          <path d="M20 28 H28 M36 28 H44" />
        </svg>);

    case "clock":
      return (
        <svg {...common}>
          <circle cx="32" cy="32" r="22" />
          <path d="M32 14 V32 L44 38" />
          <path d="M32 8 V12 M32 52 V56 M8 32 H12 M52 32 H56" />
        </svg>);

    case "whatsapp":
      return (
        <svg viewBox="0 0 32 32" width={size} height={size} fill="currentColor">
          <path d="M16 3C9 3 3.5 8.5 3.5 15.5c0 2.4.6 4.6 1.8 6.6L3 29l7-1.8c1.9 1 4 1.6 6.1 1.6 7 0 12.6-5.5 12.6-12.5S23 3 16 3zm0 22.6c-1.9 0-3.7-.5-5.3-1.4l-.4-.2-4.1 1.1 1.1-4-.3-.4a10.5 10.5 0 1 1 9 5zm5.8-7.8c-.3-.2-1.9-.9-2.2-1-.3-.1-.5-.2-.7.2s-.8 1-.9 1.2c-.2.2-.3.2-.6.1-.3-.2-1.3-.5-2.5-1.5-.9-.8-1.6-1.8-1.7-2.1-.2-.3 0-.5.1-.6l.5-.5c.2-.2.2-.3.3-.5.1-.2 0-.4 0-.5l-.7-1.8c-.2-.5-.4-.4-.6-.4h-.5c-.2 0-.5.1-.7.3-.2.2-.9.9-.9 2.2 0 1.3.9 2.5 1.1 2.7.1.2 1.8 2.7 4.4 3.8 2 .9 2.5.8 3 .8.5 0 1.5-.6 1.7-1.2.2-.6.2-1.1.2-1.2-.1-.1-.2-.2-.5-.3z" />
        </svg>);

    case "instagram":
      return (
        <svg {...common}>
          <rect x="14" y="14" width="36" height="36" rx="9" />
          <circle cx="32" cy="32" r="8" />
          <circle cx="44" cy="20" r="1.2" fill={ICON_STROKE} />
        </svg>);

    case "linkedin":
      return (
        <svg {...common}>
          <rect x="12" y="12" width="40" height="40" rx="4" />
          <path d="M22 28 V44 M22 22 V22.1" />
          <path d="M30 44 V30 M30 36 C30 32 33 30 36 30 C39 30 42 32 42 36 V44" />
        </svg>);

    case "arrow-down":
      return (
        <svg viewBox="0 0 16 16" width={size} height={size} fill="none" stroke="currentColor" strokeWidth="0.6">
          <path d="M8 2 V14 M4 10 L8 14 L12 10" strokeLinecap="round" strokeLinejoin="round" />
        </svg>);

    default:
      return null;
  }
}

// ----------------------------------------------------------------------------
// Placeholder image — striped diagonal w/ monospace label
// ----------------------------------------------------------------------------
function Placeholder({ label, ratio = "4/3", tone = "dark", overlay = true }) {
  const isDark = tone === "dark";
  const bg = isDark ? "#15140f" : "#e8e0d0";
  const stripe = isDark ? "rgba(184,148,76,0.06)" : "rgba(138,131,120,0.18)";
  const text = isDark ? "rgba(245,240,230,0.42)" : "rgba(10,10,10,0.4)";
  return (
    <div
      style={{
        position: "relative",
        width: "100%",
        aspectRatio: ratio,
        background: `repeating-linear-gradient(135deg, ${bg} 0 14px, ${stripe} 14px 15px)`,
        overflow: "hidden"
      }}>
      
      {overlay &&
      <div
        style={{
          position: "absolute",
          inset: 0,
          background: isDark ?
          "radial-gradient(120% 80% at 70% 30%, rgba(184,148,76,0.10), transparent 60%), linear-gradient(180deg, rgba(10,10,10,0.3) 0%, rgba(10,10,10,0.55) 100%)" :
          "radial-gradient(120% 80% at 30% 30%, rgba(255,255,255,0.6), transparent 60%)"
        }} />

      }
      <div
        style={{
          position: "absolute",
          left: 20,
          bottom: 20,
          fontFamily: "ui-monospace, Menlo, Consolas, monospace",
          fontSize: 10,
          letterSpacing: "0.2em",
          color: text,
          textTransform: "uppercase"
        }}>
        
        {label}
      </div>
    </div>);

}

// ----------------------------------------------------------------------------
// Hero placeholder (villa/piscine) — SVG dessiné, pas une vraie photo
// ----------------------------------------------------------------------------
function HeroVisual() {
  return (
    <div style={{ position: "absolute", inset: 0, overflow: "hidden" }}>
      {/* Photographie de fond */}
      <img
        src="https://images.unsplash.com/photo-1613490493576-7fde63acd811?w=2200&q=85&auto=format&fit=crop"
        alt=""
        aria-hidden="true"
        style={{
          position: "absolute",
          inset: 0,
          width: "100%",
          height: "100%",
          objectFit: "cover",
          objectPosition: "center 50%",
          filter: "saturate(0.9) contrast(1.06) brightness(0.78)"
        }} />
      
      <div
        style={{
          position: "absolute",
          inset: 0,
          background:
          "linear-gradient(180deg, rgba(10,10,10,0.22) 0%, rgba(10,10,10,0.32) 40%, rgba(10,10,10,0.68) 100%)"
        }} />
      
      {/* Légère teinte chaude */}
      <div
        style={{
          position: "absolute",
          inset: 0,
          background:
          "linear-gradient(140deg, rgba(184,148,76,0.10) 0%, transparent 50%, rgba(10,10,10,0.25) 100%)",
          mixBlendMode: "overlay",
          display: "none"
        }} />
      
      {/* Anciennes couches (désactivées) — conservées pour fallback */}
      <div
        style={{
          display: "none",
          position: "absolute",
          inset: 0,
          background:
          "linear-gradient(180deg, #16140e 0%, #1f1a12 40%, #2a2218 70%, #1a1812 100%)"
        }} />
      
      {/* Grain subtil */}
      <div
        style={{
          position: "absolute",
          inset: 0,
          opacity: 0.12,
          mixBlendMode: "overlay",
          background:
          "url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/%3E%3CfeColorMatrix values='0 0 0 0 0.95 0 0 0 0 0.92 0 0 0 0 0.85 0 0 0 0.4 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E\")"
        }} />
      
    </div>);

}

Object.assign(window, {
  useScrollY,
  useInView,
  Reveal,
  GoldRule,
  Eyebrow,
  MovaWordmark,
  PillButton,
  Icon,
  Placeholder,
  HeroVisual
});