import { motion } from "motion/react";
import { ChevronDown } from "lucide-react";

type FaqItem = {
  question: string;
  answer: string;
};

type FaqSplitMediaProps = {
  tag: string;
  title: string;
  description: string;
  primaryButton?: { text: string; href: string };
  secondaryButton?: { text: string; href: string };
  items: FaqItem[];
} & ({ imageSrc: string; videoSrc?: never } | { videoSrc: string; imageSrc?: never });

const FaqSplitMedia = ({
  tag,
  title,
  description,
  primaryButton,
  secondaryButton,
  items,
  imageSrc,
  videoSrc,
}: FaqSplitMediaProps) => {
  return (
    <section
      data-webild-section="FaqSplitMedia"
      aria-label="FAQ section"
      className="relative w-full py-16 md:py-24"
    >
      <div className="w-content-width mx-auto flex flex-col gap-8">
        <div className="flex flex-col items-center 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 && (
                <a
                  href={primaryButton.href}
                  className="primary-button rounded-theme h-9 px-6 inline-flex items-center justify-center text-sm text-primary-cta-text"
                >
                  {primaryButton.text}
                </a>
              )}
              {secondaryButton && (
                <a
                  href={secondaryButton.href}
                  className="secondary-button rounded-theme h-9 px-6 inline-flex items-center justify-center text-sm text-secondary-cta-text"
                >
                  {secondaryButton.text}
                </a>
              )}
            </div>
          )}
        </div>

        <div className="grid grid-cols-1 md:grid-cols-5 gap-5">
          <motion.div
            initial={{ opacity: 0, y: 20 }}
            whileInView={{ opacity: 1, y: 0 }}
            viewport={{ once: true, margin: "-10%" }}
            transition={{ duration: 0.6, ease: "easeOut" }}
            className="card relative md:col-span-2 h-80 md:h-auto rounded-theme overflow-hidden"
          >
            {videoSrc ? (
              <video
                src={videoSrc}
                autoPlay
                muted
                loop
                playsInline
                aria-label="FAQ video"
                className="absolute inset-0 size-full object-cover"
              />
            ) : (
              <img
                src={imageSrc}
                alt=""
                className="absolute inset-0 size-full object-cover"
              />
            )}
          </motion.div>

          <div className="md:col-span-3 flex flex-col gap-3">
            {items.map((item, index) => (
              <motion.details
                key={index}
                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="card rounded-theme p-4 group"
              >
                <summary className="flex items-center justify-between gap-3 cursor-pointer list-none">
                  <h3 className="text-base md:text-lg font-medium leading-tight">{item.question}</h3>
                  <ChevronDown className="size-4 shrink-0 transition-transform duration-300 group-open:rotate-180" strokeWidth={2} />
                </summary>
                <p className="mt-2 text-sm leading-tight text-muted-foreground">{item.answer}</p>
              </motion.details>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
};

export default FaqSplitMedia;
