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

type Testimonial = {
  name: string;
  handle: string;
  text: string;
  rating: number;
} & ({ imageSrc: string; videoSrc?: never } | { videoSrc: string; imageSrc?: never });

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

const HeroSplitTestimonial = ({
  tag,
  title,
  description,
  primaryButton,
  secondaryButton,
  imageSrc,
  videoSrc,
  testimonials,
}: HeroSplitTestimonialProps) => {
  const testimonial = testimonials[0];

  return (
    <section
      data-webild-section="HeroSplitTestimonial"
      aria-label="Hero section"
      className="relative flex items-center w-full h-fit md:h-svh pt-25 pb-20 md:py-0"
    >
      <div className="flex flex-col md:flex-row items-center gap-8 w-content-width mx-auto">
        <div className="w-full md:w-1/2">
          <div className="flex flex-col items-center md:items-start gap-3">
            <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.h1
              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-7xl 2xl:text-8xl font-medium text-center md:text-left text-balance"
            >
              {title}
            </motion.h1>

            <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="max-w-8/10 text-lg md:text-xl leading-tight text-center md:text-left"
            >
              {description}
            </motion.p>

            <div className="flex flex-wrap max-md:justify-center gap-3 mt-3">
              <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>
              <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>

        <motion.div
          initial={{ opacity: 0, y: 20 }}
          whileInView={{ opacity: 1, y: 0 }}
          viewport={{ once: true, margin: "-15%" }}
          transition={{ duration: 0.6, delay: 0.2, ease: "easeOut" }}
          className="relative w-full md:w-1/2 aspect-3/4 md:aspect-auto md:h-[65vh] md:max-h-[75svh] card rounded-theme overflow-hidden p-3 xl:p-4 2xl:p-5"
        >
          {videoSrc ? (
            <video
              src={videoSrc}
              autoPlay
              muted
              loop
              playsInline
              aria-label="Hero video"
              className="w-full h-full object-cover rounded-theme"
            />
          ) : (
            <img
              src={imageSrc}
              alt=""
              className="w-full h-full object-cover rounded-theme"
            />
          )}

          <figure className="absolute bottom-6 left-6 right-6 md:left-auto md:max-w-1/2 card rounded-theme p-4 flex flex-col gap-3">
            <div className="flex gap-1">
              {Array.from({ length: 5 }).map((_, i) => (
                <Star
                  key={i}
                  className={`w-5 h-5 text-accent ${i < testimonial.rating ? "fill-accent" : "fill-transparent"}`}
                  strokeWidth={1.5}
                />
              ))}
            </div>

            <blockquote className="text-base leading-tight text-balance">
              {testimonial.text}
            </blockquote>

            <figcaption className="flex items-center gap-3">
              {testimonial.videoSrc ? (
                <video
                  src={testimonial.videoSrc}
                  autoPlay
                  muted
                  loop
                  playsInline
                  className="w-10 h-10 rounded-full object-cover"
                />
              ) : (
                <img
                  src={testimonial.imageSrc}
                  alt=""
                  className="w-10 h-10 rounded-full object-cover"
                />
              )}
              <div className="flex flex-col">
                <span className="text-sm font-medium">{testimonial.name}</span>
                <span className="text-sm text-muted-foreground">{testimonial.handle}</span>
              </div>
            </figcaption>
          </figure>
        </motion.div>
      </div>
    </section>
  );
};

export default HeroSplitTestimonial;
