"use client";

import Image from "next/image";
import Link from "next/link";
import { useEffect, useRef } from "react";
import { ArrowRight, ArrowUpRight, Sun } from "lucide-react";
import { HeroSystem } from "@/components/HeroSystem";
import { gsap, registerGsap, ScrollTrigger } from "@/components/scroll/gsap";

export function Hero() {
  const sectionRef = useRef<HTMLElement>(null);

  useEffect(() => {
    const section = sectionRef.current;
    if (!section) return;
    registerGsap();
    const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
    const desktop = window.matchMedia("(min-width: 841px)");

    const ctx = gsap.context(() => {
      if (!reduced) {
        gsap.timeline({ defaults: { ease: "power3.out" } })
          .to(".hero .eyebrow:before", { scaleX: 1, duration: 0.55 }, 0.05)
          .to(".hero .eyebrow", { opacity: 1, y: 0, duration: 0.5 }, 0.08)
          .to(".hero-line-inner", { yPercent: 0, duration: 0.85, stagger: 0.14 }, 0.16)
          .to(".hero-reveal", { opacity: 1, y: 0, duration: 0.6, stagger: 0.09 }, 0.48);

        gsap.fromTo(".hero-media", { scale: 1.08 }, { scale: 1, duration: 1.8, ease: "power2.out" });

        const scrub = {
          trigger: section,
          start: "top top",
          end: "bottom top",
          scrub: 0.6,
        };
        gsap.to(".hero-parallax", { yPercent: 12, ease: "none", scrollTrigger: scrub });
        gsap.to(".hero-copy", { y: -56, ease: "none", scrollTrigger: scrub });
        if (desktop.matches) {
          gsap.to(".hero-system", { y: -24, scale: 0.94, ease: "none", scrollTrigger: scrub });
        }
      }

      const fine = window.matchMedia("(hover: hover) and (pointer: fine)").matches;
      const system = section.querySelector(".hero-system-float");
      const copy = section.querySelector(".hero-copy-float");
      if (!reduced && fine && system && copy) {
        const sx = gsap.quickTo(system, "x", { duration: 0.7, ease: "power3.out" });
        const sy = gsap.quickTo(system, "y", { duration: 0.7, ease: "power3.out" });
        const cx = gsap.quickTo(copy, "x", { duration: 0.85, ease: "power3.out" });
        const cy = gsap.quickTo(copy, "y", { duration: 0.85, ease: "power3.out" });
        const move = (event: PointerEvent) => {
          if (event.pointerType !== "mouse") return;
          const rect = section.getBoundingClientRect();
          const x = (event.clientX - rect.left) / rect.width - 0.5;
          const y = (event.clientY - rect.top) / rect.height - 0.5;
          sx(x * 28);
          sy(y * 16);
          cx(x * -12);
          cy(y * -8);
        };
        const leave = () => {
          sx(0);
          sy(0);
          cx(0);
          cy(0);
        };
        section.addEventListener("pointermove", move);
        section.addEventListener("pointerleave", leave);
        return () => {
          section.removeEventListener("pointermove", move);
          section.removeEventListener("pointerleave", leave);
        };
      }
    }, section);

    const refresh = () => ScrollTrigger.refresh();
    window.addEventListener("load", refresh);
    return () => {
      window.removeEventListener("load", refresh);
      ctx.revert();
    };
  }, []);

  return (
    <section className="hero" ref={sectionRef}>
      <div className="hero-media">
        <div className="hero-parallax">
          <Image
            className="hero-image"
            src="/images/solar-hero.jpg"
            alt="Solar panels across a modern rooftop"
            fill
            priority
            sizes="100vw"
          />
        </div>
      </div>
      <div className="hero-overlay" />
      <div className="hero-layout">
        <div className="hero-copy">
          <div className="hero-copy-float">
            <div className="hero-content">
              <span className="eyebrow light">
                <span className="status-dot" /> SOLAR & STORAGE FOR SOUTH AUSTRALIA
              </span>
              <h1>
                <span className="hero-line">
                  <span className="hero-line-inner">More from the sun.</span>
                </span>
                <span className="hero-line">
                  <span className="hero-line-inner">
                    <em>More for you.</em>
                  </span>
                </span>
              </h1>
              <p className="hero-reveal">
                Smarter solar and battery solutions for your home, your business and a brighter tomorrow.
              </p>
              <div className="hero-buttons">
                <Link href="/contact" className="button hero-reveal">
                  Get Your Free Quote <ArrowUpRight size={19} />
                </Link>
                <Link href="#solutions" className="button outline-light hero-reveal">
                  Explore Our Solutions <ArrowRight size={18} />
                </Link>
              </div>
              <div className="hero-caption hero-reveal">
                <span className="hero-sun" aria-hidden="true">
                  <Sun size={19} />
                </span>
                Local understanding. Energy designed around you.
              </div>
            </div>
          </div>
        </div>
        <HeroSystem />
      </div>
      <div className="hero-bottom shell">
        <div className="hero-bottom-row hero-reveal">
          <span>A BRIGHTER WAY FORWARD</span>
          <a className="scroll-indicator" href="#about">
            Discover the difference <span className="scroll-cue" aria-hidden="true">↓</span>
          </a>
        </div>
      </div>
    </section>
  );
}
