"use client";

import { motion, useMotionValueEvent, useScroll } from "framer-motion";
import Image from "next/image";
import { useEffect, useRef, useState } from "react";

/** Offsets are % of the stage, relative to the center logo (0, 0). */
const integrations = [
  {
    id: "slack",
    src: "/homepage/integration/Slack.svg",
    x: -26,
    y: -24,
    rotate: -16,
  },
  {
    id: "shopify-top",
    src: "/homepage/integration/Shopify.svg",
    x: 0,
    y: -42,
    rotate: 0,
  },
  {
    id: "wordpress",
    src: "/homepage/integration/wordpress.svg",
    x: 26,
    y: -25,
    rotate: 14,
  },
  {
    id: "shopify-left",
    src: "/homepage/integration/Shopify.svg",
    x: -30,
    y: 22,
    rotate: 26,
  },
  {
    id: "notion",
    src: "/homepage/integration/Notion.svg",
    x: 30,
    y: 22,
    rotate: -17,
  },
  {
    id: "zapier",
    src: "/homepage/integration/Zapier.svg",
    x: 0,
    y: 43,
    rotate: 0,
  },
];

export default function IntegrationOrbit() {
  const sectionRef = useRef<HTMLElement>(null);
  const stageRef = useRef<HTMLDivElement>(null);
  const [showBadges, setShowBadges] = useState(false);
  const [stageSize, setStageSize] = useState({ width: 0, height: 0 });

  const { scrollYProgress } = useScroll({
    target: sectionRef,
    offset: ["start end", "end start"],
  });

  useMotionValueEvent(scrollYProgress, "change", (progress) => {
    setShowBadges(progress >= 0.2);
  });

  useEffect(() => {
    const stage = stageRef.current;
    if (!stage) return;

    const updateSize = () => {
      setStageSize({
        width: stage.clientWidth,
        height: stage.clientHeight,
      });
    };

    updateSize();
    const observer = new ResizeObserver(updateSize);
    observer.observe(stage);
    return () => observer.disconnect();
  }, []);

  return (
    <section
      ref={sectionRef}
      className="relative flex items-center justify-center overflow-hidden"
    >
      <div
        ref={stageRef}
        className="relative mx-auto h-107.5 w-full max-w-212.5 sm:h-112.5"
      >
        {/* Center logo */}
        <div className="absolute left-1/2 top-1/2 z-20 -translate-x-1/2 -translate-y-1/2">
          <Image
            src="/homepage/integration/logo.png"
            alt="Chatbot logo"
            width={266}
            height={266}
            priority
            className="h-auto w-40 sm:w-46.25 md:w-51.25"
          />
        </div>

        {/* Badges start on the logo, then fly out to orbit positions */}
        {integrations.map((item, index) => {
          const targetX = (item.x / 100) * stageSize.width;
          const targetY = (item.y / 100) * stageSize.height;

          return (
            <motion.div
              key={item.id}
              className="absolute left-1/2 top-1/2 z-10"
              aria-hidden={!showBadges}
              initial={false}
              animate={
                showBadges
                  ? {
                      x: targetX,
                      y: targetY,
                      opacity: 1,
                      scale: 1,
                      rotate: item.rotate,
                    }
                  : {
                      x: 0,
                      y: 0,
                      opacity: 0,
                      scale: 0.2,
                      rotate: 0,
                    }
              }
              transition={{
                type: "spring",
                stiffness: 140,
                damping: 14,
                mass: 0.9,
                delay: showBadges ? index * 0.08 : 0,
              }}
            >
              <div className="-translate-x-1/2 -translate-y-1/2">
                <Image
                  src={item.src}
                  alt={item.id}
                  width={131}
                  height={54}
                  className="h-auto w-24 sm:w-29 md:w-32.75"
                />
              </div>
            </motion.div>
          );
        })}
      </div>
    </section>
  );
}
