/* global React, Paywall */

// Compliance map artboard. Renders the same Paywall but overlays small
// numbered tags pinned to the annotated regions (using data-anno attrs).
function PaywallAnnotated({ theme = "light" }) {
  const ref = React.useRef(null);
  const [pins, setPins] = React.useState([]);

  React.useEffect(() => {
    if (!ref.current) return;
    const root = ref.current;
    const rootRect = root.getBoundingClientRect();
    const rules = [
      { tag: "1",  selector: '[data-anno="A1t"]', side: "right", label: "iOS 3.1.2(a) · 标题 / 周期 / 单价 高于 CTA", color: "#0284C7" },
      { tag: "1",  selector: '[data-anno="A1p"]', side: "right", label: "iOS 3.1.2(a) · 三档周期 + 单价齐整", color: "#0284C7" },
      { tag: "1",  selector: '[data-anno="A1c"]', side: "right", label: "iOS 3.1.2(a) · 主 CTA 在层级之下", color: "#0284C7" },
      { tag: "1",  selector: '[data-anno="A1f"]', side: "right", label: "iOS 3.1.2 · ToS + Privacy 链接", color: "#0284C7" },
      { tag: "1",  selector: '[data-anno="A2"]',  side: "right", label: "iOS 3.1.2 · Restore Purchases", color: "#0284C7" },
      { tag: "2",  selector: '[data-anno="C2b C3c"]', side: "right", label: "Google Play · 应用内取消路径 (Play › 订阅)", color: "#7C3AED" },
      { tag: "3",  selector: '[data-anno="C3"]',  side: "right", label: "上海消保委 · 自动续费字号 ≥ 正文 + 24h 提醒", color: "#B45309" },
      { tag: "4",  selector: '[data-anno="C4a"]', side: "right", label: "未成年人保护 · 家长指导提示常驻", color: "#15803D" },
      { tag: "4",  selector: '[data-anno="C4b"]', side: "right", label: "未成年人保护 · 家长确认门禁 (短信 / 身份证末 4 / PIN)", color: "#15803D" },
      { tag: "5",  selector: '[data-anno="A1c"]', side: "left",  label: "视觉克制 · CTA 用 accent，无红色 / 倒计时", color: "#9333EA" },
    ];
    const pinList = rules.map((r, i) => {
      const els = root.querySelectorAll(r.selector);
      const el = els[r.selector === '[data-anno="A1p"]' ? Math.min(i, els.length - 1) : 0];
      if (!el) return null;
      const rect = el.getBoundingClientRect();
      return {
        ...r,
        key: i,
        top: rect.top - rootRect.top,
        height: rect.height,
        left: rect.left - rootRect.left,
        width: rect.width,
      };
    }).filter(Boolean);
    setPins(pinList);
  }, []);

  return (
    <div style={{ position: "relative", display: "inline-block" }} ref={ref}>
      <Paywall theme={theme} />

      {/* Pins overlay */}
      <div style={{ position: "absolute", inset: 0, pointerEvents: "none" }}>
        {pins.map((p) => (
          <React.Fragment key={p.key}>
            {/* circle pin on the device */}
            <div style={{
              position: "absolute",
              top: p.top + p.height / 2 - 11,
              left: p.side === "right" ? p.left + p.width - 14 : p.left - 8,
              width: 22, height: 22, borderRadius: 999,
              background: p.color, color: "#fff",
              fontFamily: '"SF Pro Rounded", system-ui, sans-serif',
              fontSize: 12, fontWeight: 700,
              display: "flex", alignItems: "center", justifyContent: "center",
              boxShadow: "0 2px 6px rgba(0,0,0,0.25)",
              border: "2px solid #fff",
            }}>{p.tag}</div>
          </React.Fragment>
        ))}
      </div>
    </div>
  );
}

window.PaywallAnnotated = PaywallAnnotated;
