import { readSiteStats } from "../_shared/stats.js";

export async function onRequestGet(context) {
  const stats = await readSiteStats(context.env);
  if (!stats) {
    return json({ ok: false, error: "missing_stats_binding" }, 500);
  }

  return json({ ok: true, stats });
}

function json(data, status = 200) {
  return new Response(JSON.stringify(data, null, 2), {
    status,
    headers: {
      "content-type": "application/json; charset=utf-8",
      "cache-control": "no-store"
    }
  });
}
