"use client";

import { useCallback } from "react";
import Link from "next/link";
import Image from "next/image";
import { ArrowUp } from "lucide-react";
import Reveal from "@/components/immersive/Reveal";
import { ScrollSmoother, prefersReducedMotion } from "@/components/immersive/gsap-setup";
import { scrollToImmersiveSection } from "@/components/immersive/immersive-scroll";

const FOOTER_LINKS = {
  Platform: [
    { label: "Portfolio Management", href: "/#product" },
    { label: "Trade Lifecycle Management", href: "/#product" },
    { label: "Reporting & Client Intelligence", href: "/#product" },
    { label: "Compliance & Regulatory", href: "/#product" },
    { label: "DeepDex Data Infrastructure", href: "/#product" },
    { label: "Product Tour", href: "/#product-tour" },
  ],
  "Who we serve": [
    { label: "Wealth Managers", href: "/#resource" },
    { label: "Asset Managers", href: "/#resource" },
    { label: "Multi-Family Offices", href: "/#resource" },
    { label: "Independent Advisors", href: "/#resource" },
  ],
  Company: [
    { label: "About Oxyfinz", href: "/about" },
    { label: "SME Initiative", href: "/sme-initiative" },
    { label: "Articles", href: "/articles" },
    { label: "Careers", href: "/careers" },
    { label: "FAQs", href: "/faq" },
  ],
  Connect: [
    { label: "Contact us", href: "/contact" },
    { label: "Book a demo", href: "/contact" },
    { label: "Log in", href: "/signin_step" },
    { label: "Sign up", href: "/register" },
  ],
};

/**
 * The immersive site footer: link columns over a bottom-anchored wordmark
 * watermark, closed by the legal bar. Shared by the landing page (inside its
 * Contact section) and every subpage rendered through ImmersivePage.
 */
export default function ImmersiveFooter() {
  // The smoother owns the scroll position where one exists (the landing
  // page), so returning to the top has to go through its scrollTo — both a
  // native scrollTo and a gsap.to(smoother, { scrollTop }) tween race the
  // smoother's own sync loop and play the journey twice (see
  // immersive-scroll.ts). Subpages have no smoother and fall back cleanly.
  const handleBackToTop = useCallback(() => {
    const smoother = ScrollSmoother.get();
    if (smoother && !prefersReducedMotion()) {
      smoother.scrollTo(0, true);
    } else {
      window.scrollTo({ top: 0, behavior: prefersReducedMotion() ? "auto" : "smooth" });
    }
  }, []);

  return (
    <footer className="relative mt-20 border-t border-white/10 pt-12 lg:mt-28">
      {/* Closing wordmark: a watermark behind the footer content, anchored
          to the page's bottom edge. The link columns and the bottom bar
          float over it — it underlays, it never takes its own band.
          This and the bar sit at the very bottom of the page, where the
          default `top 88%` trigger line can be unreachable — they fire on
          entering the viewport instead, so nothing is stranded hidden. */}
      <Reveal
        y={40}
        start="top bottom"
        className="pointer-events-none absolute inset-x-0 bottom-0 select-none overflow-hidden"
      >
        <p
          aria-hidden
          className="immersive-watermark font-heading whitespace-nowrap text-center text-[clamp(3.5rem,12.5vw,11rem)] font-semibold uppercase leading-none tracking-[-0.045em]"
        >
          Oxyfinz
        </p>
      </Reveal>

      <Reveal stagger={0.08} className="relative grid grid-cols-2 gap-8 lg:grid-cols-4">
        {Object.entries(FOOTER_LINKS).map(([category, links]) => (
          <div key={category} data-reveal>
            <h3 className="immersive-label immersive-label-accent mb-5">{category}</h3>
            <ul className="flex flex-col gap-2">
              {links.map((link) => {
                // Landing-section links carry a hash ("/#product"). Next's
                // own Link scroll-to-hash and our ScrollSmoother-aware scroll
                // would otherwise both fire on the landing page — a visible
                // double scroll. scroll={false} disables Next's; the onClick
                // is the only scroll that runs. Plain page links (About,
                // Contact, …) are untouched and keep Next's default
                // scroll-to-top-of-new-page behaviour.
                const sectionId = link.href.startsWith("/#") ? link.href.slice(2) : null;
                return (
                  <li key={link.label}>
                    <Link
                      href={link.href}
                      scroll={sectionId ? false : undefined}
                      onClick={
                        sectionId
                          ? (e) => {
                              if (scrollToImmersiveSection(sectionId)) e.preventDefault();
                            }
                          : undefined
                      }
                      className="group inline-flex items-center text-sm text-[hsl(var(--muted-foreground))] transition-colors duration-200 hover:text-[hsl(var(--foreground))]"
                    >
                      {/* Hairline that grows in on hover — quieter than an
                          underline. Zero footprint at rest so the column edge
                          stays flush. */}
                      <span
                        aria-hidden
                        className="h-px w-0 bg-[hsl(var(--primary))] transition-all duration-200 group-hover:mr-1.5 group-hover:w-3"
                      />
                      <span>{link.label}</span>
                    </Link>
                  </li>
                );
              })}
            </ul>
          </div>
        ))}
      </Reveal>

      <Reveal
        y={16}
        start="top bottom"
        className="relative mt-16 flex flex-col gap-6 border-t border-white/10 pt-8 lg:mt-20 lg:flex-row lg:items-center lg:justify-between"
      >
        <div className="flex flex-wrap items-center gap-4">
          <Image
            src="/assets/logooxy.png"
            alt="Oxyfinz"
            width={140}
            height={46}
            className="h-7 w-auto object-contain"
          />
          <span className="immersive-label">© 2026 Oxyfinz. All rights reserved.</span>
        </div>

        <div className="flex flex-wrap items-center gap-x-6 gap-y-3">
          <nav className="flex flex-wrap items-center gap-x-6 gap-y-2" aria-label="Legal">
            <Link href="/privacy" className="immersive-label transition-colors hover:text-[hsl(var(--foreground))]">
              Privacy
            </Link>
            <Link href="/terms" className="immersive-label transition-colors hover:text-[hsl(var(--foreground))]">
              Terms
            </Link>
            <Link href="/cookies" className="immersive-label transition-colors hover:text-[hsl(var(--foreground))]">
              Cookies
            </Link>
            <Link href="/refund" className="immersive-label transition-colors hover:text-[hsl(var(--foreground))]">
              Refunds
            </Link>
          </nav>
          <button
            type="button"
            onClick={handleBackToTop}
            className="group inline-flex items-center gap-2 rounded-md border border-white/12 px-3.5 py-2 transition-colors hover:border-white/30"
          >
            <span className="immersive-label transition-colors group-hover:text-[hsl(var(--foreground))]">
              Back to top
            </span>
            <ArrowUp
              className="size-3.5 text-[hsl(var(--muted-foreground))] transition-transform duration-200 group-hover:-translate-y-0.5 group-hover:text-[hsl(var(--foreground))]"
              aria-hidden
            />
          </button>
        </div>
      </Reveal>
    </footer>
  );
}
