/* FEATURES — 4-card grid, staggered scale+fade entrances */

const { motion: _m, useInView: _uiv } = window.Motion || {};

const CARD_VIDEO = "https://d8j0ntlcm91z4.cloudfront.net/user_38xzZboKViGWJOttwIXH07lWA1P/hf_20260406_133058_0504132a-0cf3-4450-a370-8ea3b05c95d4.mp4";

/* Abstract placeholder icons rendered as small SVG glyphs for each card.
   We avoid fetching third-party image CDN URLs directly for reliability.
   Each icon has its own distinct silhouette so cards feel individual. */
function IconCitation({ className = "" }) {
  return (
    <svg viewBox="0 0 40 40" className={className} aria-hidden="true">
      <defs>
        <linearGradient id="ic1" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0" stopColor="#DEDBC8" stopOpacity="0.95" />
          <stop offset="1" stopColor="#7A7865" stopOpacity="0.6" />
        </linearGradient>
      </defs>
      <rect x="1" y="1" width="38" height="38" rx="9" fill="#1a1a19" stroke="rgba(222,219,200,0.18)" />
      <path d="M10 16c0-3 2-5 5-5h3v4h-3c-1 0-2 .7-2 2v2h5v7h-8v-10z" fill="url(#ic1)" />
      <path d="M22 16c0-3 2-5 5-5h3v4h-3c-1 0-2 .7-2 2v2h5v7h-8v-10z" fill="url(#ic1)" />
    </svg>
  );
}
function IconSEO({ className = "" }) {
  return (
    <svg viewBox="0 0 40 40" className={className} aria-hidden="true">
      <rect x="1" y="1" width="38" height="38" rx="9" fill="#1a1a19" stroke="rgba(222,219,200,0.18)" />
      <circle cx="17" cy="18" r="7" fill="none" stroke="#DEDBC8" strokeWidth="2" />
      <path d="M22.5 23.5 L29 30" stroke="#DEDBC8" strokeWidth="2.5" strokeLinecap="round" />
      <circle cx="17" cy="18" r="2.2" fill="#DEDBC8" />
    </svg>
  );
}
function IconAutomation({ className = "" }) {
  return (
    <svg viewBox="0 0 40 40" className={className} aria-hidden="true">
      <rect x="1" y="1" width="38" height="38" rx="9" fill="#1a1a19" stroke="rgba(222,219,200,0.18)" />
      <circle cx="20" cy="20" r="4" fill="none" stroke="#DEDBC8" strokeWidth="2" />
      <g stroke="#DEDBC8" strokeWidth="2" strokeLinecap="round">
        <path d="M20 9 V12" />
        <path d="M20 28 V31" />
        <path d="M9 20 H12" />
        <path d="M28 20 H31" />
        <path d="M12.5 12.5 L14.5 14.5" />
        <path d="M25.5 25.5 L27.5 27.5" />
        <path d="M27.5 12.5 L25.5 14.5" />
        <path d="M14.5 25.5 L12.5 27.5" />
      </g>
    </svg>
  );
}

function Checklist({ items }) {
  return (
    <ul className="space-y-2.5 sm:space-y-3">
      {items.map((it, i) => (
        <li key={i} className="flex items-start gap-2.5 sm:gap-3">
          <span className="mt-[3px] shrink-0 text-primary">
            <Check size={14} strokeWidth={2.5} />
          </span>
          <span className="text-gray-400 text-xs sm:text-[13px] leading-snug">{it}</span>
        </li>
      ))}
    </ul>
  );
}

function LearnMore({ label = "Learn more" }) {
  return (
    <a
      href="#"
      className="group mt-auto inline-flex items-center gap-2 text-primary text-xs sm:text-sm pt-6"
    >
      <span className="border-b border-primary/40 group-hover:border-primary transition-colors">{label}</span>
      <span className="inline-flex items-center justify-center w-7 h-7 rounded-full border border-primary/40 group-hover:border-primary transition-colors">
        <ArrowRight size={12} style={{ transform: "rotate(-45deg)", color: "#DEDBC8" }} />
      </span>
    </a>
  );
}

function FeatureCard({ index, children, className = "" }) {
  const { motion, useInView } = window.Motion;
  const ref = React.useRef(null);
  const inView = useInView(ref, { once: true, margin: "-100px" });
  return (
    <motion.div
      ref={ref}
      initial={{ opacity: 0, scale: 0.95 }}
      animate={inView ? { opacity: 1, scale: 1 } : { opacity: 0, scale: 0.95 }}
      transition={{ duration: 0.8, delay: index * 0.15, ease: EASE_CARD }}
      className={`relative rounded-2xl overflow-hidden ${className}`}
    >
      {children}
    </motion.div>
  );
}

function VideoCard({ index }) {
  const videoRef = React.useRef(null);
  React.useEffect(() => {
    const v = videoRef.current;
    if (!v) return;
    const tryPlay = () => { const p = v.play(); if (p && p.catch) p.catch(() => {}); };
    tryPlay();
    v.addEventListener("loadeddata", tryPlay);
    v.addEventListener("canplay", tryPlay);
    return () => {
      v.removeEventListener("loadeddata", tryPlay);
      v.removeEventListener("canplay", tryPlay);
    };
  }, []);
  return (
    <FeatureCard index={index} className="lg:h-full h-[360px] bg-[#151515]">
      <video
        ref={videoRef}
        src={CARD_VIDEO}
        autoPlay
        loop
        muted
        playsInline
        preload="auto"
        crossOrigin="anonymous"
        className="absolute inset-0 w-full h-full object-cover"
      />
      <div
        className="absolute inset-0 pointer-events-none"
        style={{
          background:
            "radial-gradient(100% 70% at 30% 30%, #2a2824 0%, #0a0907 70%, #000 100%)",
          zIndex: -1,
        }}
      />
      <div className="absolute inset-0 bg-gradient-to-b from-black/10 via-transparent to-black/70 pointer-events-none" />
      <div className="absolute inset-0 flex items-end p-5 sm:p-6">
        <div className="text-base sm:text-lg md:text-xl font-medium tracking-tight" style={{ color: "#E1E0CC" }}>
          Ranking, by design.
        </div>
      </div>
    </FeatureCard>
  );
}

function ContentCard({ index, numberLabel, title, items, Icon }) {
  return (
    <FeatureCard index={index} className="lg:h-full h-auto bg-[#212121] flex flex-col p-5 sm:p-6">
      <div className="flex items-start justify-between mb-8 md:mb-10">
        <div className="w-10 h-10 sm:w-12 sm:h-12">
          <Icon className="w-full h-full" />
        </div>
        <span className="text-primary/50 text-xs font-mono tracking-widest">{numberLabel}</span>
      </div>

      <h3 className="text-primary text-lg sm:text-xl md:text-[22px] font-medium leading-tight tracking-tight mb-5 md:mb-6">
        {title}
      </h3>

      <Checklist items={items} />

      <LearnMore />
    </FeatureCard>
  );
}

function Features() {
  const cards = [
    {
      kind: "content",
      numberLabel: "01",
      title: "AI Citation Visibility.",
      items: [
        "Cited by ChatGPT, Gemini & Perplexity",
        "Structured data & schema markup",
        "Conversational query capture",
        "Competitive citation displacement",
      ],
      Icon: IconCitation,
    },
    {
      kind: "content",
      numberLabel: "02",
      title: "Google SEO System.",
      items: [
        "Technical SEO & Core Web Vitals",
        "Topical authority & content architecture",
        "Digital PR & link acquisition",
      ],
      Icon: IconSEO,
    },
    {
      kind: "content",
      numberLabel: "03",
      title: "Business Automation.",
      items: [
        "CRM flows & lead nurturing sequences",
        "24/7 data pipelines & qualification",
        "Visibility dashboard & monthly reporting",
      ],
      Icon: IconAutomation,
    },
  ];

  return (
    <section className="relative min-h-screen bg-black px-4 md:px-6 pb-16 md:pb-24">
      <div className="absolute inset-0 bg-noise opacity-[0.15] pointer-events-none" />

      <div className="relative z-10 pt-16 md:pt-24 pb-10 md:pb-14 max-w-6xl mx-auto">
        <WordsPullUpMultiStyle
          className="text-xl sm:text-2xl md:text-3xl lg:text-4xl font-normal leading-[1.1] tracking-tight"
          wrapperStyle={{ justifyContent: "flex-start" }}
          segments={[
            { text: "One system for every search surface.", className: "text-primary" },
            { text: "Built to rank. Built to be cited. Built to compound.", className: "text-gray-500" },
          ]}
          stagger={0.06}
        />
      </div>

      <div className="relative z-10 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 lg:h-[480px] gap-3 sm:gap-2 md:gap-1">
        <VideoCard index={0} />
        {cards.map((c, i) => (
          <ContentCard
            key={c.numberLabel}
            index={i + 1}
            numberLabel={c.numberLabel}
            title={c.title}
            items={c.items}
            Icon={c.Icon}
          />
        ))}
      </div>
    </section>
  );
}

Object.assign(window, { Features });
