// ============================================================
// Cyber Runner — real data binding + Logo + shared primitives
// Reads window.CR_DATA (injected server-side from Postgres) and
// exposes the same globals the section components expect.
// ============================================================
const CR = window.CR_DATA || {};

// Feature flag: badges are hidden for now while the core journey is clarified.
// Flip to true to bring the Badges section, nav/footer links, and profile badges back.
const SHOW_BADGES = false;

// ---- Two-tone vector logo (recolorable via CSS vars) -------
function CyberRunnerLogo({ className = "", style = {} }) {
  return (
    <svg className={"cr-logo " + className} viewBox="220 432 640 220" style={style} role="img" aria-label="Cyber Runner">
      <g className="lg-figure">
        <path d="M354.56,531.33c-3.31-0.08-6.09-1.15-8.42-3.67c-3.66-3.96-7.39-7.87-11.33-11.53c-5.87-5.46-10.85-5.28-16.5,0.26c-5.04,4.95-9.97,10.02-15.15,14.82c-3.62,3.35-7.95,3.85-12.42,1.64c-4.33-2.15-5.88-6.01-5.81-10.55c0.04-2.44,1.15-4.5,3.03-6.3c9.78-9.39,19.48-18.88,29.16-28.37c5.92-5.8,12.62-5.99,18.67-0.21c3.49,3.34,6.63,7.04,10.07,10.43c5.81,5.73,11.02,5.82,16.91,0.26c4.46-4.21,8.67-8.68,13.07-12.95c4.88-4.73,11.92-5.11,15.91-0.97c4.5,4.67,4.11,11.16-1,16.21c-9.26,9.14-18.57,18.23-27.76,27.44C360.59,530.26,357.89,531.44,354.56,531.33z"></path>
        <path d="M324.33,541.75c3.18-0.19,5.93,0.84,8.34,3.32c8.29,8.54,16.75,16.92,25.09,25.42c6.58,6.71,6.48,13.36-0.1,19.87c-10.59,10.47-21.18,20.93-31.7,31.46c-3.88,3.88-8.49,3.83-12.92,2.02c-4.47-1.83-5.6-6.11-5.41-10.65c0.12-2.76,1.71-4.78,3.64-6.68c6.23-6.14,12.39-12.35,18.54-18.57c5.13-5.19,5.33-10.28,0.33-15.56c-4.35-4.59-9.09-8.81-13.46-13.38c-3.27-3.41-3.88-7.55-2-11.89C316.44,543,319.89,541.56,324.33,541.75z"></path>
        <path d="M233.84,629.49c-0.26-3.09,1.1-5.49,3.38-7.72c16.71-16.44,33.35-32.95,49.97-49.48c3.77-3.75,7.98-5.53,13.1-3.27c4.4,1.94,7.01,5.42,7.58,10.24c0.34,2.9-0.41,5.48-2.59,7.63c-17.48,17.25-34.95,34.51-52.38,51.81c-3.69,3.66-8.13,3.93-12.35,2C236.12,638.66,233.07,635.1,233.84,629.49z"></path>
      </g>
      <circle className="lg-dot" cx="354.77" cy="454.89" r="16.76"></circle>
      <g className="lg-runner">
        <path d="M431.81,613.81h-6.11v-42.79h32.09c2.95,0,5.48,1.04,7.56,3.13s3.13,4.61,3.13,7.56v6.11c0,2.96-1.04,5.47-3.12,7.55c-0.04,0.04-0.07,0.07-0.09,0.09c0.02,0.02,0.05,0.04,0.09,0.06c2.08,2.1,3.12,4.63,3.12,7.58v10.7h-6.11v-10.7c0-1.26-0.45-2.34-1.34-3.24c-0.9-0.9-1.98-1.34-3.24-1.34h-25.98V613.81z M431.81,592.42h25.98c1.26,0,2.34-0.45,3.24-1.34c0.9-0.9,1.34-1.98,1.34-3.24v-6.11c0-1.26-0.45-2.34-1.34-3.24s-1.98-1.35-3.24-1.35h-25.98V592.42z"></path>
        <path d="M480.72,603.11c0,1.26,0.45,2.34,1.34,3.24s1.98,1.34,3.24,1.34h21.4c1.26,0,2.34-0.45,3.24-1.34s1.34-1.98,1.34-3.24v-32.09h6.11v32.09c0,2.96-1.04,5.48-3.13,7.57s-4.61,3.13-7.56,3.13h-21.4c-2.96,0-5.48-1.04-7.57-3.13s-3.13-4.61-3.13-7.57v-32.09h6.11V603.11z"></path>
        <path d="M529.63,578.29v35.52h-6.11v-42.79h6.88l29.8,35.52v-35.52h6.11v42.79h-6.88L529.63,578.29z"></path>
        <path d="M578.53,578.29v35.52h-6.11v-42.79h6.88l29.8,35.52v-35.52h6.11v42.79h-6.88L578.53,578.29z"></path>
        <path d="M627.44,595.47v7.64c0,1.26,0.45,2.34,1.34,3.24s1.98,1.34,3.24,1.34h32.09v6.11h-32.09c-2.96,0-5.48-1.04-7.57-3.13s-3.13-4.61-3.13-7.57v-21.4c0-2.95,1.04-5.48,3.13-7.56s4.61-3.13,7.57-3.13h32.09v6.11h-32.09c-1.26,0-2.34,0.45-3.24,1.35s-1.34,1.98-1.34,3.24v7.64H658v6.11H627.44z"></path>
        <path d="M676.34,613.81h-6.11v-42.79h32.09c2.95,0,5.48,1.04,7.56,3.13s3.13,4.61,3.13,7.56v6.11c0,2.96-1.04,5.47-3.12,7.55c-0.04,0.04-0.07,0.07-0.09,0.09c0.02,0.02,0.05,0.04,0.09,0.06c2.08,2.1,3.12,4.63,3.12,7.58v10.7h-6.11v-10.7c0-1.26-0.45-2.34-1.34-3.24c-0.9-0.9-1.98-1.34-3.24-1.34h-25.98V613.81z M676.34,592.42h25.98c1.26,0,2.34-0.45,3.24-1.34c0.9-0.9,1.34-1.98,1.34-3.24v-6.11c0-1.26-0.45-2.34-1.34-3.24s-1.98-1.35-3.24-1.35h-25.98V592.42z"></path>
      </g>
      <g className="lg-cyber">
        <path d="M504.11,536.73v10.86h-57.03c-5.25,0-9.73-1.86-13.44-5.57c-3.71-3.71-5.57-8.19-5.57-13.44v-38.02c0-5.25,1.85-9.73,5.57-13.44c3.71-3.71,8.19-5.57,13.44-5.57h57.03v10.86h-57.03c-2.25,0-4.16,0.8-5.76,2.39c-1.59,1.59-2.39,3.51-2.39,5.76v38.02c0,2.25,0.8,4.17,2.39,5.76c1.59,1.59,3.51,2.39,5.76,2.39H504.11z"></path>
        <path d="M544.85,510.34l-0.65-0.76l-31.94-38.02h12.22l25.8,30.74l25.8-30.74h12.22l-31.88,38.02l-0.71,0.81v37.2h-10.86V510.34z"></path>
        <path d="M596.45,471.56h57.03c5.25,0,9.73,1.86,13.44,5.57c3.71,3.71,5.57,8.19,5.57,13.44V496c0,5.25-1.85,9.72-5.54,13.42c-0.07,0.07-0.13,0.13-0.16,0.16c0.04,0.04,0.09,0.07,0.16,0.11c3.69,3.73,5.54,8.22,5.54,13.47v5.43c0,5.25-1.86,9.73-5.57,13.44c-3.71,3.71-8.19,5.57-13.44,5.57h-57.03V471.56z M607.31,504.15h46.17c2.25,0,4.16-0.8,5.76-2.39c1.59-1.59,2.39-3.51,2.39-5.76v-5.43c0-2.24-0.8-4.16-2.39-5.76c-1.59-1.59-3.51-2.39-5.76-2.39h-46.17V504.15z M607.31,515.01v21.73h46.17c2.25,0,4.16-0.8,5.76-2.39c1.59-1.59,2.39-3.51,2.39-5.76v-5.43c0-2.25-0.8-4.16-2.39-5.76c-1.59-1.59-3.51-2.39-5.76-2.39H607.31z"></path>
        <path d="M694.21,515.01v13.58c0,2.25,0.8,4.17,2.39,5.76c1.59,1.59,3.51,2.39,5.76,2.39h57.03v10.86h-57.03c-5.25,0-9.73-1.86-13.44-5.57c-3.71-3.71-5.57-8.19-5.57-13.44v-38.02c0-5.25,1.85-9.73,5.57-13.44c3.71-3.71,8.19-5.57,13.44-5.57h57.03v10.86h-57.03c-2.25,0-4.16,0.8-5.76,2.39c-1.59,1.59-2.39,3.51-2.39,5.76v13.58h54.31v10.86H694.21z"></path>
        <path d="M781.11,547.6h-10.86v-76.04h57.03c5.25,0,9.73,1.86,13.44,5.57c3.71,3.71,5.57,8.19,5.57,13.44v10.86c0,5.25-1.85,9.72-5.54,13.42c-0.07,0.07-0.13,0.13-0.16,0.16c0.04,0.04,0.09,0.07,0.16,0.11c3.69,3.73,5.54,8.22,5.54,13.47v19.01h-10.86v-19.01c0-2.24-0.8-4.16-2.39-5.76c-1.59-1.59-3.51-2.39-5.76-2.39h-46.17V547.6z M781.11,509.58h46.17c2.25,0,4.16-0.8,5.76-2.39c1.59-1.59,2.39-3.51,2.39-5.76v-10.86c0-2.24-0.8-4.16-2.39-5.76c-1.59-1.59-3.51-2.39-5.76-2.39h-46.17V509.58z"></path>
      </g>
    </svg>
  );
}

// ---- Real data from the server -------------------------------
const MEMBERS    = CR.members    || [];
const BADGES     = CR.badges     || [];
const TEAM       = CR.team       || { distance: 0, runs: 0, elevation: 0, members: 0, hours: 0 };
const FUN_STATS  = CR.funStats   || [];
const VALUES     = CR.values     || [];
const CHALLENGES = CR.challenges || [];
const GOAL       = CR.goal       || { targetKm: 5000, label: "5,000 km this month", month: "" };
const FEED_DATA  = CR.feed       || [];
const CLUB_NAME  = CR.clubName   || "Cyber Runner";
const MONTHNAV   = CR.month      || { label: CR.monthLabel || "", prev: null, next: null, isCurrent: true };
const ATTENDANCE = CR.attendance || { labels: [], rows: [] };
const BADGE_MAP  = Object.fromEntries(BADGES.map(b => [b.id, b]));

// ---- Leaderboard config (operates on real MEMBERS) -----------
function paceLabel(sec) {
  if (!sec) return "—";
  const mm = Math.floor(sec / 60), ss = String(sec % 60).padStart(2, "0");
  return `${mm}:${ss}`;
}
const LEADERBOARDS = {
  distance: { label: "Most Distance", unit: "km",   get: m => m.km,       fmt: v => v.toFixed(1) },
  sessions: { label: "Most Sessions", unit: "runs", get: m => m.sessions, fmt: v => String(v) },
  longest:  { label: "Longest Run",   unit: "km",   get: m => m.longest,  fmt: v => v.toFixed(1) },
};
function rankBy(key) {
  const cfg = LEADERBOARDS[key];
  return [...MEMBERS].sort((a, b) => cfg.get(b) - cfg.get(a));
}

// ---- Live refresh from the JSON endpoint ---------------------
function fetchClubData() {
  return fetch("/api/club-data", { headers: { "Accept": "application/json" } }).then(r => r.json());
}

Object.assign(window, {
  CyberRunnerLogo, MEMBERS, BADGES, BADGE_MAP, TEAM, LEADERBOARDS, rankBy,
  FUN_STATS, VALUES, CHALLENGES, GOAL, FEED_DATA, CLUB_NAME, MONTHNAV, ATTENDANCE, paceLabel, fetchClubData,
  SHOW_BADGES,
});
