import { useEffect, useState } from "react";
import type { LucideIcon } from "lucide-react";
import { motion, AnimatePresence } from "motion/react";
import Button from "@/components/ui/Button";
import HeroBackgroundSlot from "@/components/ui/HeroBackgroundSlot";
import TextAnimation from "@/components/ui/TextAnimation";
import ImageOrVideo from "@/components/ui/ImageOrVideo";
import ScrollReveal from "@/components/ui/ScrollReveal";
import ActiveBadge from "@/components/ui/ActiveBadge";
import { resolveIcon } from "@/utils/resolve-icon";

type FeatureItem = {
  icon: string | LucideIcon;
  title: string;
  description: string;
};

type HeroBillboardFeaturesProps = {
  badge: string;
  title: string;
  description: string;
  primaryButton: { text: string; href: string };
  secondaryButton: { text: string; href: string };
  features: FeatureItem[];
} & ({ imageSrc: string; videoSrc?: never } | { videoSrc: string; imageSrc?: never });

const INTERVAL = 5000;

const HeroBillboardFeatures = ({
  badge,
  title,
  description,
  primaryButton,
  secondaryButton,
  imageSrc,
  videoSrc,
  features,
}: HeroBillboardFeaturesProps) => {
  const [currentIndex, setCurrentIndex] = useState(0);

  useEffect(() => {
    if (features.length <= 1) return;

    const interval = setInterval(() => {
      setCurrentIndex((prev) => (prev + 1) % features.length);
    }, INTERVAL);
    return () => clearInterval(interval);
  }, [features.length]);

  const feature = features[currentIndex];
  const FeatureIcon = resolveIcon(feature.icon);

  return (
    <section aria-label="Hero section" className="relative pt-25 pb-20 md:py-30 mb-20">
      <HeroBackgroundSlot />
      <div className="flex flex-col gap-10 w-content-width mx-auto">
        <div className="flex flex-col items-center gap-2 text-center">
          <ActiveBadge text={badge} className="mb-1" />

          <TextAnimation
            text={title}
            variant="fade"
            gradientText={true}
            tag="h1"
            className="text-6xl font-medium text-balance"
          />

          <TextAnimation
            text={description}
            variant="fade"
            gradientText={false}
            tag="p"
            className="text-base md:text-lg leading-tight text-balance"
          />

          <div className="flex flex-wrap justify-center gap-3 mt-3">
            <Button text={primaryButton.text} href={primaryButton.href} variant="primary"/>
            <Button text={secondaryButton.text} href={secondaryButton.href} variant="secondary" animationDelay={0.1} />
          </div>
        </div>

        <ScrollReveal variant="fade-blur" delay={0.2} className="relative w-full p-3 xl:p-4 2xl:p-5 card rounded overflow-hidden">
          <ImageOrVideo imageSrc={imageSrc} videoSrc={videoSrc} className="aspect-3/4 md:aspect-video" />

          <AnimatePresence mode="wait">
            <motion.div
              key={currentIndex}
              initial={{ opacity: 0, y: 10 }}
              animate={{ opacity: 1, y: 0 }}
              exit={{ opacity: 0, y: -10 }}
              transition={{ duration: 0.3 }}
              className="absolute top-6 right-6 xl:top-8 xl:right-8 2xl:top-10 2xl:right-10 max-w-xs p-3 xl:p-4 2xl:p-5 card rounded flex flex-col gap-2"
            >
              <FeatureIcon className="size-5 text-accent mb-0.5" strokeWidth={1.5} />
              <p className="text-base font-medium leading-tight">{feature.title}</p>
              <p className="text-sm text-foreground/75 leading-tight">{feature.description}</p>
            </motion.div>
          </AnimatePresence>
        </ScrollReveal>
      </div>
    </section>
  );
};

export default HeroBillboardFeatures;
