"use client";

import Link from "next/link";
import { useEffect, useState, useSyncExternalStore } from "react";
import { createPortal } from "react-dom";
import { FOCUS_RING_CLASSES } from "@/components/ui/styles";
import { cn } from "@/lib/utils";

/**
 * "Get your ticket!" badge — traced verbatim (shape + transforms) from
 * `cta_signaturee1.svg`, inlined here instead of used via `<img src>` for
 * one reason: an `<img>`-loaded SVG is an isolated document that can't
 * inherit the page's fonts, so the asset's own baked-in
 * `font-family:'RushenCurvy-Regular'` just fell back to the browser
 * default. Inlining it as real markup lets the `<text>` elements below use
 * `var(--font-rushen-curvy)` (the real font, self-hosted via
 * next/font/local — see layout.tsx) instead.
 */
function TicketCtaBadge({ className }: { className?: string }) {
  return (
    <svg viewBox="0 0 2954 1582" className={className} aria-hidden="true" focusable="false">
      <g transform="matrix(1,0,0,1,-3921.600963,-2249.999796)">
        <g transform="matrix(4.166667,0,0,4.166667,0,1581.6125)">
          <path
            fill="rgb(255,218,50)"
            d="M1356.8,535.546C1356.8,535.546 1161.58,533.081 1132.65,532.718C1101.2,532.317 1069.76,531.928 1038.31,531.527C1025.1,531.365 1011.9,531.185 998.67,531.032C986.217,530.873 973.738,530.506 961.272,530.552C961.145,530.548 961.027,530.555 960.899,530.551C960.88,530.549 960.869,530.558 960.86,530.547L960.822,530.543C957.512,530.551 954.514,529.208 952.149,527.089C952.106,527.035 952.043,526.979 951.978,526.943C950.31,525.468 949.016,523.62 948.174,521.586C943.865,518.923 941.039,514.054 941.19,508.719C941.614,492.878 942.251,477.059 942.9,461.232C943.602,444.35 944.385,427.457 944.479,410.569C945.055,404.111 942.776,397.258 945.444,391.153C946.735,388.002 949.025,385.783 951.742,384.445C953.244,379.598 957.199,375.594 962.406,374.465C962.428,374.447 962.447,374.449 962.477,374.443C962.557,374.432 962.628,374.41 962.699,374.388C964.841,373.921 966.952,373.372 969.058,372.773C972.593,371.68 976.055,370.431 979.452,368.97C982.539,367.563 985.563,366.012 988.487,364.294C990.991,362.736 993.421,361.043 995.765,359.242C997.594,357.75 999.346,356.161 1001.02,354.494C1002.49,352.915 1003.89,351.288 1005.21,349.573C1006.18,348.205 1007.09,346.78 1007.94,345.311C1008.58,344.073 1009.17,342.811 1009.71,341.513C1010.1,340.436 1010.44,339.344 1010.74,338.236C1010.96,337.132 1011.16,336.033 1011.3,334.929C1011.37,333.984 1011.4,333.055 1011.4,332.112C1011.36,331.263 1011.28,330.42 1011.16,329.573C1011,328.731 1010.81,327.906 1010.6,327.068C1010.32,326.193 1010.01,325.325 1009.65,324.462C1009.23,323.562 1008.77,322.688 1008.29,321.821C1007.73,320.957 1007.13,320.107 1006.51,319.294C1005.64,318.258 1004.75,317.259 1003.81,316.294C1002.64,315.216 1001.43,314.203 1000.18,313.224C998.541,312.066 996.821,310.988 995.084,309.976C993.01,308.9 990.912,307.948 988.749,307.068C986.009,306.066 983.224,305.208 980.394,304.492C977.017,303.756 973.63,303.206 970.187,302.807C967.678,302.577 965.161,302.424 962.632,302.389L962.593,302.384C962.552,302.4 962.513,302.395 962.474,302.391C955.046,302.261 948.991,296.005 948.488,288.803C945.121,284.583 944.169,278.363 945.573,272.78C947.466,251.126 948.627,229.423 951.024,207.823C953.705,197.487 949.264,183.156 955.83,174.582C956.065,167.547 961.753,160.745 969.097,160.482C975.049,160.277 981.045,160.587 987.007,160.657C995.245,160.756 1003.5,160.876 1011.75,160.966C1028,161.169 1044.24,161.391 1060.49,161.594L1157.95,162.835C1190.99,163.26 1224.03,163.724 1257.08,164.11C1290.82,164.502 1324.56,164.971 1358.3,165.402C1392.43,165.836 1426.57,166.289 1460.69,166.712C1492.47,167.109 1524.25,167.526 1556.03,167.923C1576.78,168.206 1597.55,168.413 1618.29,168.735C1618.3,168.726 1618.3,168.726 1618.32,168.728C1618.36,168.732 1618.39,168.726 1618.43,168.73C1621.81,168.788 1625.17,168.815 1628.54,168.863C1633.86,168.933 1638.46,172.254 1640.8,176.787C1641.09,176.995 1641.38,177.222 1641.66,177.458C1645.66,180.844 1646.88,185.678 1647.36,190.668C1647.74,194.656 1647.96,198.657 1648.12,202.66C1648.49,211.448 1648.73,220.242 1648.88,229.027C1649.13,244.833 1649.28,260.638 1649.57,276.438C1649.69,282.852 1649.78,289.265 1649.72,295.68C1649.71,298.015 1649.46,300.218 1648.89,302.503C1647.49,308.106 1642.38,312.045 1637.15,313.781C1634.02,314.81 1630.93,315.883 1627.91,317.149C1626.57,317.771 1625.26,318.435 1623.94,319.139C1622.11,320.297 1620.34,321.531 1618.59,322.835C1616.66,324.464 1614.81,326.181 1613.01,327.971C1611.87,329.213 1610.76,330.478 1609.68,331.776C1608.57,333.395 1607.52,335.04 1606.51,336.719C1605.61,338.458 1604.77,340.204 1603.96,342.003C1603.37,343.687 1602.85,345.379 1602.37,347.115C1602.01,348.932 1601.69,350.753 1601.41,352.589C1601.33,353.935 1601.27,355.274 1601.26,356.628C1601.34,357.598 1601.43,358.551 1601.53,359.514C1601.88,361.329 1602.28,363.099 1602.74,364.877C1603.39,366.901 1604.12,368.884 1604.93,370.856C1605.65,372.426 1606.42,373.962 1607.25,375.475C1607.62,376.045 1608.01,376.607 1608.41,377.151C1608.87,377.682 1609.37,378.206 1609.85,378.719C1610.3,379.101 1610.75,379.484 1611.21,379.857C1612.24,380.567 1613.29,381.229 1614.37,381.866C1615.65,382.504 1616.95,383.105 1618.27,383.639C1619.82,384.198 1621.38,384.68 1622.96,385.085C1624.79,385.478 1626.63,385.783 1628.48,385.992C1630.98,386.211 1633.51,386.286 1636.01,386.29C1643.94,386.316 1650.3,393.587 1650.04,401.297C1649.96,403.88 1649.2,406.234 1647.97,408.252C1650.03,413.717 1649.04,420.69 1648.65,426.589C1647.93,438.735 1647.13,450.885 1646.51,463.042C1645.65,478.768 1644.99,494.516 1644.72,510.286C1644.88,521.506 1647.11,538.407 1631.76,539.872C1629.45,540.136 1627.15,539.76 1625.02,538.884C1624.65,538.913 1624.27,538.921 1623.91,538.912C1609.11,538.728 1594.31,538.543 1579.53,538.35C1551.91,537.987 1524.28,537.644 1496.66,537.3C1468.12,536.956 1439.57,536.592 1411.03,536.228L1370.72,535.72L1356.8,535.546Z"
          />
        </g>
        <g transform="matrix(4.166667,0,0,4.166667,0,1601.383333)">
          <path
            fill="rgb(249,107,1)"
            fillRule="nonzero"
            d="M1491.45,519.453C1487.83,519.166 1484.38,517.38 1482.02,514.561C1479.69,511.774 1478.7,508.34 1478.83,504.764C1479.46,485.219 1479.99,465.68 1480.59,446.159C1481.49,416.482 1482.37,386.792 1483.25,357.12C1484.17,326.216 1485.11,295.297 1486.04,264.375C1486.43,251.33 1486.83,238.266 1487.22,225.211C1487.56,213.784 1488.08,202.346 1488.26,190.914C1488.27,190.564 1488.26,190.222 1488.29,189.874C1488.56,182.457 1495.01,176.215 1502.12,177.594C1509.58,179.041 1508.56,187.719 1508.66,190.993C1508.66,191.289 1508.63,191.571 1508.61,191.865C1508.63,191.867 1508.65,191.87 1508.67,191.872C1508.48,205.528 1507.85,219.179 1507.44,232.831C1506.97,248.498 1506.5,264.176 1506.03,279.843C1505.11,310.378 1504.17,340.919 1503.28,371.457C1502.41,401.186 1501.51,430.892 1500.6,460.605C1500.12,475.995 1499.71,491.366 1499.22,506.754C1499.08,510.688 1497.94,515.487 1494.98,517.759"
          />
        </g>
        <g transform="matrix(4.166144,0.065975,-0.065975,4.166144,0,0)" style={{ fontFamily: "var(--font-rushen-curvy)" }}>
          <text x="1214.125px" y="643.423px" style={{ fontSize: 128, fill: "rgb(249,107,1)", stroke: "white", strokeWidth: 0.06 }}>
            g<tspan x="1297.581px 1369.901px" y="643.423px 643.423px">et</tspan>
          </text>
        </g>
        <g transform="matrix(4.166144,0.065975,-0.065975,4.166144,0,0)" style={{ fontFamily: "var(--font-rushen-curvy)" }}>
          <text x="1121.581px" y="755.423px" style={{ fontSize: 128, fill: "rgb(249,107,1)", stroke: "white", strokeWidth: 0.06 }}>
            y<tspan x="1196.461px 1289.517px 1369.517px" y="755.423px 755.423px 755.423px">our</tspan>
          </text>
        </g>
        <g transform="matrix(4.166144,0.065975,-0.065975,4.166144,0,0)" style={{ fontFamily: "var(--font-rushen-curvy)" }}>
          <text x="996.781px" y="867.424px" style={{ fontSize: 128, fill: "rgb(249,107,1)", stroke: "white", strokeWidth: 0.06 }}>
            t<tspan x="1075.117px 1104.429px 1184.557px 1264.045px 1336.365px 1415.981px" y="867.424px 867.424px 867.424px 867.424px 867.424px 867.424px">icket!</tspan>
          </text>
        </g>
      </g>
    </svg>
  );
}

/**
 * Sticky "Get your ticket!" CTA — stays `position: fixed` bottom-right for
 * the whole Signature page, same technique as the homepage HeroBanner's
 * floating CTA (portaled to `<body>` so no ancestor's transform/isolate
 * stacking context can break `fixed`). Hides right as the site Footer
 * (`#site-footer`, rendered globally by the root layout) starts entering
 * the viewport, so it never overlaps the footer.
 */
export function SignatureFloatingCta({ href }: { href: string }) {
  // Client-only mount flag without a setState-in-effect (react-hooks/
  // set-state-in-effect) — useSyncExternalStore's server snapshot is always
  // false, client snapshot always true, no subscription ever fires.
  const mounted = useSyncExternalStore(
    () => () => {},
    () => true,
    () => false,
  );

  const [visible, setVisible] = useState(true);
  useEffect(() => {
    let rafId = 0;
    function check() {
      rafId = 0;
      const footer = document.getElementById("site-footer");
      if (!footer) return;
      setVisible(footer.getBoundingClientRect().top > window.innerHeight);
    }
    function onScroll() {
      if (!rafId) rafId = requestAnimationFrame(check);
    }

    check();
    window.addEventListener("scroll", onScroll, { passive: true });
    window.addEventListener("resize", onScroll);
    return () => {
      window.removeEventListener("scroll", onScroll);
      window.removeEventListener("resize", onScroll);
      if (rafId) cancelAnimationFrame(rafId);
    };
  }, []);

  if (!mounted) return null;

  return createPortal(
    <Link
      href={href}
      aria-label="View event details"
      className={cn(
        "fixed bottom-6 right-6 z-40 -rotate-12 transition-[opacity,transform] duration-300 ease-out hover:-translate-y-2 hover:rotate-0 hover:scale-110",
        FOCUS_RING_CLASSES,
      )}
      style={{ opacity: visible ? 1 : 0, pointerEvents: visible ? "auto" : "none" }}
    >
      <TicketCtaBadge className="h-16 w-auto sm:h-20 lg:h-24" />
    </Link>,
    document.body,
  );
}
