import { motion } from "motion/react";

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

interface FeaturesDetailedStepsProps {
  tag: string;
  title: string;
  description: string;
  primaryButton?: { text: string; href: string };
  secondaryButton?: { text: string; href: string };
  steps: StepItem[];
}

const FeaturesDetailedSteps = ({
  tag,
  title,
  description,
  primaryButton,
  secondaryButton,
  steps,
}: FeaturesDetailedStepsProps) => {
  return (
    <section
      data-webild-section="FeaturesDetailedSteps"
      aria-label="Features detailed steps 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="flex flex-col w-content-width mx-auto gap-5">
          {steps.map((step, index) => {
            const stepNumber = String(index + 1).padStart(2, "0");
            const tilt = index % 2 === 0 ? "rotate-3" : "-rotate-3";
            return (
              <motion.div
                key={step.title}
                initial={{ opacity: 0, y: 20 }}
                whileInView={{ opacity: 1, y: 0 }}
                viewport={{ once: true, margin: "-10%" }}
                transition={{ duration: 0.6, delay: 0.2, ease: "easeOut" }}
                className="flex flex-col md:flex-row justify-between 2xl:w-8/10 mx-auto gap-5 md:gap-8 p-5 md:p-8 card rounded-theme overflow-hidden"
              >
                <div className="flex flex-col justify-between w-full md:w-1/2">
                  <div className="flex flex-col gap-5">
                    <span className="w-fit card rounded-full px-3 py-1 mb-1 text-sm">{step.tag}</span>
                    <h3 className="text-5xl md:text-8xl font-medium leading-none">{step.title}</h3>
                  </div>
                  <div className="block md:hidden w-full h-px my-5 bg-foreground/20" />
                  <div className="flex flex-col gap-2 md:gap-5">
                    <h4 className="text-2xl md:text-3xl font-medium text-balance">{step.subtitle}</h4>
                    <p className="text-base md:text-lg leading-tight text-balance">{step.description}</p>
                  </div>
                </div>
                <div className="flex flex-col w-full md:w-35/100 gap-8">
                  <span className="hidden md:block self-end text-8xl font-medium">{stepNumber}</span>
                  <div className={`aspect-square rounded-theme overflow-hidden ${tilt}`}>
                    {step.videoSrc ? (
                      <video
                        src={step.videoSrc}
                        autoPlay
                        muted
                        loop
                        playsInline
                        aria-label="Step video"
                        className="w-full h-full object-cover"
                      />
                    ) : (
                      <img
                        src={step.imageSrc}
                        alt=""
                        className="w-full h-full object-cover"
                      />
                    )}
                  </div>
                </div>
              </motion.div>
            );
          })}
        </div>
      </div>
    </section>
  );
};

export default FeaturesDetailedSteps;
