"use client";

import { motion } from "motion/react";

const ease = [0.22, 1, 0.36, 1] as const;

export type PageHeroProps = {
  title: string;
  subtitle: string;
  backgroundSrc?: string;
};

function AnimatedTitle({ text }: { text: string }) {
  const words = text.split(" ");
  return (
    <h1 className="text-[28px] font-bold leading-[1.3] sm:text-[48px] lg:text-[60px]">
      <span className="flex flex-wrap justify-center gap-x-[0.3em] gap-y-1">
        {words.map((word, i) => (
          <motion.span
            key={i}
            initial={{ opacity: 0, y: 22, filter: "blur(8px)" }}
            animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
            transition={{ duration: 0.65, ease, delay: 0.3 + i * 0.09 }}
            className="inline-block"
          >
            {word}
          </motion.span>
        ))}
      </span>
    </h1>
  );
}

export function PageHero({ title, subtitle, backgroundSrc }: PageHeroProps) {
  const wordCount = title.split(" ").length;

  return (
    <section className="px-4 pt-6 sm:px-6 lg:px-6">
      <motion.div
        initial={{ opacity: 0, scale: 0.97 }}
        animate={{ opacity: 1, scale: 1 }}
        transition={{ duration: 0.75, ease }}
        className="relative mx-auto flex min-h-[200px] max-w-[1440px] items-center justify-center overflow-hidden rounded-[24px] bg-[linear-gradient(110deg,#35c4b9_0%,#1e7c73_54%,#0d4c47_100%)] px-5 py-12 text-center sm:min-h-[270px] sm:rounded-[32px] sm:px-8 sm:py-16 lg:min-h-[320px]"
      >
        {/* Optional background image — Ken Burns */}
        {backgroundSrc && (
          <motion.div
            className="absolute inset-0"
            initial={{ scale: 1.08 }}
            animate={{ scale: 1 }}
            transition={{ duration: 8, ease: "easeOut" }}
          >
            <div
              className="absolute inset-0 bg-cover bg-center"
              style={{ backgroundImage: `url(${backgroundSrc})` }}
            />
          </motion.div>
        )}

        {/* Overlay */}
        <motion.div
          className="absolute inset-0 bg-black/10"
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          transition={{ duration: 1.2, ease: "easeOut" }}
        />

        {/* Ambient orb — top end */}
        <motion.div
          aria-hidden
          className="pointer-events-none absolute rounded-full"
          style={{
            width: 400,
            height: 400,
            right: -120,
            top: -120,
            background: "radial-gradient(ellipse, rgba(53,196,185,0.25) 0%, transparent 70%)",
            filter: "blur(60px)",
          }}
          initial={{ opacity: 0, scale: 0.6 }}
          animate={{ opacity: 1, scale: 1 }}
          transition={{ duration: 2.2, ease: "easeOut", delay: 0.2 }}
        />

        {/* Ambient orb — bottom start */}
        <motion.div
          aria-hidden
          className="pointer-events-none absolute rounded-full"
          style={{
            width: 300,
            height: 300,
            left: -80,
            bottom: -80,
            background: "radial-gradient(ellipse, rgba(13,76,71,0.4) 0%, transparent 70%)",
            filter: "blur(50px)",
          }}
          initial={{ opacity: 0 }}
          animate={{ opacity: 1 }}
          transition={{ duration: 2.4, ease: "easeOut", delay: 0.4 }}
        />

        {/* Brand stripes — start corner · desktop only (fixed px positions from Figma 1440px canvas) */}
        {([
          { left: -38, top: 47,  delay: 0.50 },
          { left: -134, top: 20, delay: 0.62 },
          { left: -52, top: 185, delay: 0.74 },
        ] as const).map((p, i) => (
          <motion.div
            key={`ls-${i}`}
            aria-hidden
            className="pointer-events-none absolute hidden items-center justify-center sm:flex"
            style={{ left: p.left, top: p.top, width: 294, height: 294 }}
            initial={{ opacity: 0, x: -28, y: -28 }}
            animate={{ opacity: 1, x: 0, y: 0 }}
            transition={{ duration: 0.75, ease, delay: p.delay }}
          >
            <div className="h-[343px] w-[72px] rotate-45 rounded-[157.5px] bg-white/[0.08]" />
          </motion.div>
        ))}

        {/* Brand stripes — end corner · desktop only */}
        {([
          { right: 23,  top: 43,  delay: 0.56 },
          { right: -73, top: 16,  delay: 0.68 },
          { right: 9,   top: 181, delay: 0.80 },
        ] as const).map((p, i) => (
          <motion.div
            key={`rs-${i}`}
            aria-hidden
            className="pointer-events-none absolute hidden items-center justify-center sm:flex"
            style={{ right: p.right, top: p.top, width: 294, height: 294 }}
            initial={{ opacity: 0, x: 28, y: -28 }}
            animate={{ opacity: 1, x: 0, y: 0 }}
            transition={{ duration: 0.75, ease, delay: p.delay }}
          >
            <div className="h-[343px] w-[72px] rotate-[135deg] rounded-[157.5px] bg-white/[0.08]" />
          </motion.div>
        ))}

        {/* Decorative rings — desktop only (too large for mobile viewports) */}
        <motion.div
          aria-hidden
          className="pointer-events-none absolute hidden rounded-full border border-white/10 sm:block"
          style={{ width: 300, height: 300 }}
          initial={{ opacity: 0, scale: 0.7 }}
          animate={{ opacity: 1, scale: 1 }}
          transition={{ duration: 1.8, ease, delay: 0.4 }}
        />
        <motion.div
          aria-hidden
          className="pointer-events-none absolute hidden rounded-full border border-white/6 sm:block"
          style={{ width: 460, height: 460 }}
          initial={{ opacity: 0, scale: 0.7 }}
          animate={{ opacity: 1, scale: 1 }}
          transition={{ duration: 2.0, ease, delay: 0.5 }}
        />

        {/* Content */}
        <div className="relative z-10 flex max-w-[820px] flex-col items-center gap-3 px-2 text-white sm:gap-4 sm:px-0">
          <AnimatedTitle text={title} />
          <motion.p
            className="text-[14px] font-medium leading-[1.6] text-white/90 sm:text-[20px] sm:leading-[1.5] lg:text-[26px]"
            initial={{ opacity: 0, y: 14, filter: "blur(6px)" }}
            animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
            transition={{ duration: 0.6, ease, delay: 0.4 + wordCount * 0.08 }}
          >
            {subtitle}
          </motion.p>
        </div>
      </motion.div>
    </section>
  );
}
