import { motion } from "motion/react";

type FeatureItem = {
  title: string;
  description: string;
} & ({ imageSrc: string; videoSrc?: never } | { videoSrc: string; imageSrc?: never });

interface FeaturesMediaCardsProps {
  tag: string;
  title: string;
  description: string;
  primaryButton?: { text: string; href: string };
  secondaryButton?: { text: string; href: string };
  items: FeatureItem[];
}

const FeaturesMediaCards = ({
  tag,
  title,
  description,
  primaryButton,
  secondaryButton,
  items,
}: FeaturesMediaCardsProps) => {
  return (
    <section
      data-webild-section="FeaturesMediaCards"
      aria-label="Features section"
      className="relative w-full py-20"
    >
      <div className="flex flex-col gap-8">
        <div className="flex flex-col items-center w-content-width mx-auto gap-2">
          <motion.span
            initial={{ opacity: 0, y: 20 }}
            whileInView={{ opacity: 1, y: 0 }}
            viewport={{ once: true, margin: "-15%" }}
            transition={{ duration: 0.6, ease: "easeOut" }}
            className="card rounded-full px-3 py-1 mb-1 text-sm"
          >
            {tag}
          </motion.span>

          <motion.h2
            initial={{ opacity: 0, y: 20 }}
            whileInView={{ opacity: 1, y: 0 }}
            viewport={{ once: true, margin: "-15%" }}
            transition={{ duration: 0.6, delay: 0.05, ease: "easeOut" }}
            className="text-6xl font-medium text-center text-balance"
          >
            {title}
          </motion.h2>

          <motion.p
            initial={{ opacity: 0, y: 20 }}
            whileInView={{ opacity: 1, y: 0 }}
            viewport={{ once: true, margin: "-15%" }}
            transition={{ duration: 0.6, delay: 0.15, ease: "easeOut" }}
            className="md:max-w-6/10 text-lg leading-tight text-center"
          >
            {description}
          </motion.p>

          {(primaryButton || secondaryButton) && (
            <div className="flex flex-wrap justify-center gap-3 mt-3">
              {primaryButton && (
                <motion.a
                  href={primaryButton.href}
                  initial={{ opacity: 0, y: 20 }}
                  whileInView={{ opacity: 1, y: 0 }}
                  viewport={{ once: true, margin: "-15%" }}
                  transition={{ duration: 0.6, delay: 0.25, ease: "easeOut" }}
                  className="primary-button rounded-theme h-9 px-6 inline-flex items-center justify-center text-sm text-primary-cta-text"
                >
                  {primaryButton.text}
                </motion.a>
              )}
              {secondaryButton && (
                <motion.a
                  href={secondaryButton.href}
                  initial={{ opacity: 0, y: 20 }}
                  whileInView={{ opacity: 1, y: 0 }}
                  viewport={{ once: true, margin: "-15%" }}
                  transition={{ duration: 0.6, delay: 0.35, ease: "easeOut" }}
                  className="secondary-button rounded-theme h-9 px-6 inline-flex items-center justify-center text-sm text-secondary-cta-text"
                >
                  {secondaryButton.text}
                </motion.a>
              )}
            </div>
          )}
        </div>

        <div className="grid grid-cols-1 md:grid-cols-3 gap-5 w-content-width mx-auto">
          {items.map((item, index) => (
            <motion.div
              key={item.title}
              initial={{ opacity: 0, y: 20 }}
              whileInView={{ opacity: 1, y: 0 }}
              viewport={{ once: true, margin: "-10%" }}
              transition={{ duration: 0.6, delay: 0.1 + index * 0.05, ease: "easeOut" }}
              className="flex flex-col gap-4 p-4 md:p-5 h-full card rounded-theme"
            >
              <div className="aspect-square rounded-theme overflow-hidden">
                {item.videoSrc ? (
                  <video
                    src={item.videoSrc}
                    autoPlay
                    muted
                    loop
                    playsInline
                    aria-label="Feature video"
                    className="w-full h-full object-cover"
                  />
                ) : (
                  <img
                    src={item.imageSrc}
                    alt=""
                    className="w-full h-full object-cover"
                  />
                )}
              </div>
              <div className="flex flex-col gap-1">
                <h3 className="text-2xl font-medium leading-tight">{item.title}</h3>
                <p className="text-sm leading-tight">{item.description}</p>
              </div>
            </motion.div>
          ))}
        </div>
      </div>
    </section>
  );
};

export default FeaturesMediaCards;
