#!/usr/bin/env node

const baseUrl = process.env.HERMES_BASE_URL || "http://localhost:8787";
const sections = argValue("--section")
  .split(",")
  .map((section) => section.trim())
  .filter(Boolean);

const result = await fetchJson(`${baseUrl}/api/health`);
const output = sections.length ? pickSections(result, sections) : result;
console.log(JSON.stringify(output, null, 2));

function argValue(name) {
  const direct = process.argv.find((arg) => arg.startsWith(`${name}=`));
  if (direct) return direct.slice(name.length + 1);
  const index = process.argv.indexOf(name);
  return index >= 0 ? process.argv[index + 1] || "" : "";
}

function pickSections(value, wanted) {
  const out = {
    ok: value.ok,
    activeRuns: value.activeRuns
  };
  for (const section of wanted) {
    if (section === "tools") out.tools = value.tools;
    else if (section === "scraper") {
      out.scraper = {
        googleMapsScrapper: value.tools?.googleMapsScrapper,
        scraplingEnricher: value.tools?.scraplingEnricher,
        ownerRabbitHole: value.tools?.ownerRabbitHole
      };
    } else if (section === "provider" || section === "minimax") {
      out.minimaxConfigured = value.minimaxConfigured;
      out.minimaxQueue = value.minimaxQueue;
    } else if (section in value) {
      out[section] = value[section];
    }
  }
  return out;
}

async function fetchJson(url) {
  const response = await fetch(url);
  if (!response.ok) {
    throw new Error(`${url} returned HTTP ${response.status}`);
  }
  return response.json();
}
