// ── Raw HTTP wrappers ─────────────────────────────────────────────────────────
// These functions only perform fetch() calls and return the raw Response or
// parsed JSON. No state mutations, no DOM access.

export async function apiFetchConfig(team) {
  const res = await fetch(`/api/config?team=${encodeURIComponent(team)}`);
  if (!res.ok) throw new Error("config_fetch_failed");
  return res.json();
}

export async function apiFetchScores() {
  const res = await fetch("/api/scores");
  if (!res.ok) throw new Error("scores_fetch_failed");
  return res.json();
}

/** Returns the raw Response so the caller can inspect status codes (410, etc.). */
export async function apiFetchGrid(seed) {
  return fetch(`/api/grid/${encodeURIComponent(seed)}`);
}

/** Returns the raw Response so the caller can inspect status codes (409, 410, etc.). */
export async function apiRevealCell(seed, x, y, team) {
  return fetch("/api/cell/reveal", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ seed, x, y, team }),
  });
}

export async function apiLogin(username, password) {
  return fetch("/api/auth/login", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ username, password }),
  });
}

export async function apiRegister(username, email, password, team) {
  return fetch("/api/auth/register", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ username, email, password, team }),
  });
}

export async function apiGetMe(token) {
  return fetch("/api/auth/me", {
    headers: { Authorization: `Bearer ${token}` },
  });
}

export async function apiFetchEconScores() {
  const res = await fetch("/api/econ-scores");
  if (!res.ok) throw new Error("econ_scores_fetch_failed");
  return res.json();
}

export async function apiTickEconScores(seed, blue, red) {
  const res = await fetch("/api/econ-scores/tick", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ seed, blue, red }),
  });
  if (!res.ok) throw new Error("econ_tick_failed");
  return res.json();
}

export async function apiFetchElementBonus() {
  const res = await fetch("/api/element-bonus");
  if (!res.ok) throw new Error("element_bonus_fetch_failed");
  return res.json();
}

export async function apiTickElementBonus(seed, blue, red) {
  const res = await fetch("/api/element-bonus/tick", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ seed, blue, red }),
  });
  if (!res.ok) throw new Error("element_bonus_tick_failed");
  return res.json();
}

export async function apiFetchDbInfo() {
  const res = await fetch("/api/db-info");
  if (!res.ok) throw new Error("db_info_fetch_failed");
  return res.json();
}

export async function apiFetchVictoryPoints() {
  const res = await fetch("/api/victory-points");
  if (!res.ok) throw new Error("vp_fetch_failed");
  return res.json();
}
