// ============== data: API-backed with mock fallback ==============

const MOCK_ROLES = [
  { id: "W3J-0421", uuid: null, title: "Senior Protocol Engineer", company: "Top-3 L1 by TVL", chain: "Solana", stack: ["Rust", "Anchor", "SPL"], type: "Full-time", level: "Senior", remote: "Remote · Global", min: 240, max: 320, cat: "Protocol", posted: 2 },
  { id: "W3J-0418", uuid: null, title: "Head of Smart Contracts", company: "Tier-1 DeFi protocol", chain: "EVM", stack: ["Solidity", "Foundry", "Vyper"], type: "Full-time", level: "Lead", remote: "Hybrid · NYC", min: 280, max: 380, cat: "DeFi", posted: 4 },
  { id: "W3J-0415", uuid: null, title: "Quantitative Researcher", company: "Prop trading firm", chain: "Multi-chain", stack: ["Python", "Rust", "MEV"], type: "Full-time", level: "Senior", remote: "On-site · Chicago", min: 320, max: 480, cat: "Trading", posted: 5 },
  { id: "W3J-0412", uuid: null, title: "Founding Designer", company: "Stealth-mode L2", chain: "EVM", stack: ["Figma", "Framer", "Brand"], type: "Full-time", level: "Lead", remote: "Remote · Americas", min: 200, max: 280, cat: "Infrastructure", posted: 6 },
  { id: "W3J-0409", uuid: null, title: "Director of BD", company: "Major restaking protocol", chain: "EVM", stack: ["BD", "Partnerships"], type: "Full-time", level: "Director", remote: "Remote · Global", min: 220, max: 340, cat: "DeFi", posted: 7 },
  { id: "W3J-0406", uuid: null, title: "Senior Backend Engineer", company: "Top wallet provider", chain: "Multi-chain", stack: ["Go", "TypeScript", "gRPC"], type: "Full-time", level: "Senior", remote: "Remote · EMEA", min: 200, max: 280, cat: "Infrastructure", posted: 9 },
  { id: "W3J-0402", uuid: null, title: "DAO Operations Lead", company: "Top governance DAO", chain: "EVM", stack: ["Governance", "Treasury"], type: "Full-time", level: "Lead", remote: "Remote · Global", min: 160, max: 220, cat: "DAO", posted: 11 },
  { id: "W3J-0398", uuid: null, title: "Staff Security Researcher", company: "Top auditing firm", chain: "Multi-chain", stack: ["Solidity", "Audit", "Fuzzing"], type: "Full-time", level: "Staff", remote: "Remote · Global", min: 280, max: 400, cat: "Security", posted: 12 },
  { id: "W3J-0395", uuid: null, title: "Founding Engineer", company: "Series A perps DEX", chain: "Solana", stack: ["Rust", "TypeScript"], type: "Full-time", level: "Lead", remote: "On-site · SF", min: 240, max: 360, cat: "DeFi", posted: 14 },
  { id: "W3J-0392", uuid: null, title: "Head of Marketing", company: "Tier-1 L1 foundation", chain: "Multi-chain", stack: ["Brand", "Comms"], type: "Full-time", level: "Director", remote: "Hybrid · London", min: 220, max: 320, cat: "Infrastructure", posted: 16 },
  { id: "W3J-0388", uuid: null, title: "Senior Frontend Engineer", company: "Major NFT marketplace", chain: "EVM", stack: ["TypeScript", "React", "viem"], type: "Full-time", level: "Senior", remote: "Remote · Americas", min: 180, max: 240, cat: "Gaming/NFT", posted: 18 },
  { id: "W3J-0385", uuid: null, title: "Cryptography Engineer", company: "Top ZK protocol", chain: "Multi-chain", stack: ["Rust", "Circom", "Halo2"], type: "Full-time", level: "Senior", remote: "Remote · Global", min: 260, max: 380, cat: "Protocol", posted: 21 },
  { id: "W3J-0381", uuid: null, title: "Game Economy Designer", company: "Top onchain game", chain: "Solana", stack: ["Tokenomics", "Sims"], type: "Full-time", level: "Senior", remote: "Remote · Global", min: 180, max: 260, cat: "Gaming/NFT", posted: 24 },
  { id: "W3J-0378", uuid: null, title: "Head of People", company: "Series B infra co.", chain: "Multi-chain", stack: ["Recruiting", "Ops"], type: "Full-time", level: "Director", remote: "Hybrid · NYC", min: 200, max: 280, cat: "Infrastructure", posted: 26 },
  { id: "W3J-0374", uuid: null, title: "MEV Research Lead", company: "Major searcher firm", chain: "EVM", stack: ["Rust", "MEV", "Statistics"], type: "Full-time", level: "Lead", remote: "Remote · Global", min: 320, max: 500, cat: "Trading", posted: 28 },
  { id: "W3J-0370", uuid: null, title: "Solidity Engineer", company: "Top stablecoin issuer", chain: "EVM", stack: ["Solidity", "Foundry"], type: "Full-time", level: "Mid", remote: "Remote · EMEA", min: 160, max: 220, cat: "DeFi", posted: 30 },
];

function daysSince(iso) {
  if (!iso) return 0;
  return Math.max(0, Math.round((Date.now() - new Date(iso).getTime()) / 86400000));
}

function normalizeRole(r) {
  return {
    id: r.public_id,
    uuid: r.id,
    title: r.title,
    company: r.company_display,
    chain: r.chain,
    stack: r.stack || [],
    type: r.type,
    level: r.level,
    remote: r.remote,
    min: r.min_comp_k,
    max: r.max_comp_k,
    cat: r.category,
    posted: daysSince(r.posted_at || r.created_at),
    description: r.description,
    requirements: r.requirements || [],
    offer: r.offer || [],
  };
}

function useRoles() {
  const [state, setState] = React.useState({ roles: [], loading: true, source: "api" });
  React.useEffect(() => {
    let alive = true;
    window.W3J_API.apiFetch("/roles", { auth: false })
      .then((data) => {
        if (!alive) return;
        setState({ roles: (data || []).map(normalizeRole), loading: false, source: "api" });
      })
      .catch(() => { if (alive) setState({ roles: MOCK_ROLES, loading: false, source: "mock" }); });
    return () => { alive = false; };
  }, []);
  return state;
}

const TICKER_ITEMS = [
  { title: "Senior Protocol Engineer", chain: "Solana", comp: "$240–320k" },
  { title: "Head of Smart Contracts", chain: "EVM", comp: "$280–380k" },
  { title: "Quant Researcher", chain: "Multi", comp: "$320–480k" },
  { title: "Founding Designer", chain: "L2", comp: "$200–280k" },
  { title: "Director of BD", chain: "Restaking", comp: "$220–340k" },
  { title: "Senior Backend", chain: "Wallet", comp: "$200–280k" },
  { title: "DAO Ops Lead", chain: "Governance", comp: "$160–220k" },
  { title: "Staff Security Researcher", chain: "Audit", comp: "$280–400k" },
  { title: "Cryptography Engineer", chain: "ZK", comp: "$260–380k" },
  { title: "MEV Research Lead", chain: "EVM", comp: "$320–500k" },
];

const STATS = [
  { num: "100", unit: "+", label: "Senior placements" },
  { num: "23", unit: "d", label: "Median time-to-hire" },
  { num: "$120", unit: "–250k", label: "Median total comp" },
];

const PARTNERS = [
  { name: "L1 Protocol", tag: "PROTOCOL · 2024" },
  { name: "Liquid staking", tag: "DEFI · 2024" },
  { name: "Perps DEX", tag: "DEFI · 2024" },
  { name: "ZK rollup", tag: "INFRA · 2024" },
  { name: "Restaking", tag: "DEFI · 2024" },
  { name: "Wallet", tag: "INFRA · 2023" },
  { name: "MEV firm", tag: "TRADING · 2023" },
  { name: "Stablecoin", tag: "DEFI · 2023" },
  { name: "NFT market", tag: "NFT · 2023" },
  { name: "Audit firm", tag: "SECURITY · 2023" },
  { name: "DAO platform", tag: "DAO · 2023" },
  { name: "Onchain game", tag: "GAMING · 2023" },
];

const HOW_STEPS = [
  { num: "01", t: "Discovery", d: "Executive onboarding. We map the role to your protocol's actual constraints.", time: "DAY 0–2" },
  { num: "02", t: "Sourcing", d: "We work warm. The shortlist comes from operators we've shipped with, not LinkedIn keyword searches.", time: "DAY 2–10" },
  { num: "03", t: "Screening", d: "Two-stage technical + cultural pass. Every candidate reaches you pre-vetted, with comp expectations aligned.", time: "DAY 7–14" },
  { num: "04", t: "Placement", d: "We mediate offer construction: base, token grants, vesting, unlock cliffs. No surprises at signing.", time: "DAY 14–23" },
  { num: "05", t: "Follow-up", d: "We check in at 30, 60, 90, 180. If retention dips, we replace. We have skin in the game.", time: "POST-START" },
];

const CHAIN_FILTERS = ["Solana", "EVM", "Bitcoin", "Multi-chain"];
const TYPE_FILTERS = ["Protocol", "DeFi", "Trading", "Infrastructure", "Security", "DAO", "Gaming/NFT"];
const LEVEL_FILTERS = ["Mid", "Senior", "Lead", "Staff", "Director"];
const REMOTE_FILTERS = ["Remote", "Hybrid", "On-site"];

window.W3J = {
  MOCK_ROLES, useRoles, normalizeRole,
  TICKER_ITEMS, STATS, PARTNERS, HOW_STEPS,
  CHAIN_FILTERS, TYPE_FILTERS, LEVEL_FILTERS, REMOTE_FILTERS,
};
