// Cookie consent banner + consent gate (opt-in). Ported from rcv.vc.
// No non-essential/analytics cookies fire until the visitor clicks Accept.
// Choice persists 180 days via a first-party cookie + localStorage mirror.
// Re-open any time via window.rfOpenCookieSettings() (footer "Cookie settings").
// NOTE: working consent mechanism, not legal advice — confirm categories and
// wording with counsel (PIPEDA, BC PIPA, Quebec Law 25, GDPR).

const CONSENT_KEY = "rf_cookie_consent";           // "accepted" | "declined"
const CONSENT_MAX_AGE = 60 * 60 * 24 * 180;        // 180 days

function readConsent() {
  const m = document.cookie.match(/(?:^|;\s*)rf_cookie_consent=(accepted|declined)/);
  if (m) return m[1];
  try { return localStorage.getItem(CONSENT_KEY); } catch (e) { return null; }
}
function writeConsent(value) {
  document.cookie = CONSENT_KEY + "=" + value + ";path=/;max-age=" + CONSENT_MAX_AGE + ";SameSite=Lax";
  try { localStorage.setItem(CONSENT_KEY, value); } catch (e) {}
}

let analyticsLoaded = false;
function loadAnalytics() {
  if (analyticsLoaded) return;
  analyticsLoaded = true;
  // --- Drop analytics here. Only runs after the visitor clicks Accept. ---
  // Cloudflare Web Analytics is cookieless and can run without this gate;
  // add GA4 or similar here if you ever need cookie-based analytics.
}

function CookieConsent() {
  const [visible, setVisible] = React.useState(false);
  React.useEffect(() => {
    const choice = readConsent();
    if (!choice) setVisible(true);
    else if (choice === "accepted") loadAnalytics();
    window.rfOpenCookieSettings = () => setVisible(true);
    return () => { delete window.rfOpenCookieSettings; };
  }, []);

  function choose(value) {
    writeConsent(value);
    if (value === "accepted") loadAnalytics();
    setVisible(false);
  }
  if (!visible) return null;

  const ghost = {
    background: "transparent", color: "rgba(255,255,255,0.7)",
    border: "1px solid rgba(255,255,255,0.28)", padding: "8px 16px",
    borderRadius: 4, cursor: "pointer",
    font: "600 13px 'Open Sans', sans-serif", letterSpacing: "-0.01em",
  };
  return (
    <div role="dialog" aria-label="Cookie notice" style={{
      position: "fixed", left: 0, right: 0, bottom: 0, zIndex: 1000,
      background: "#33501a", color: "#fff",
      borderTop: "1px solid rgba(255,255,255,0.12)", padding: "16px 24px",
    }}>
      <div style={{ maxWidth: 1200, margin: "0 auto", display: "flex", alignItems: "center", justifyContent: "space-between", gap: 24, flexWrap: "wrap" }}>
        <p style={{ margin: 0, fontSize: 13, lineHeight: 1.5, color: "rgba(255,255,255,0.75)", maxWidth: 720, letterSpacing: "0.01em" }}>
          We use cookies to operate this site and, with your consent, to understand how it is used. You can accept or decline non-essential cookies, or read our{" "}
          <a href="privacy.html" style={{ color: "#cdd529", textDecoration: "none" }}>privacy policy</a>.
        </p>
        <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
          <button onClick={() => choose("declined")} style={ghost}>Decline</button>
          <button onClick={() => choose("accepted")} style={{
            background: "#cdd529", color: "#33501a", border: 0, padding: "9px 20px",
            borderRadius: 4, cursor: "pointer",
            font: "700 13px 'Open Sans', sans-serif", letterSpacing: "-0.01em",
          }}>Accept</button>
        </div>
      </div>
    </div>
  );
}
window.CookieConsent = CookieConsent;
