/**
 * Status module setup — collects hosts and network targets to monitor.
 * Does NOT install anything; returns the status config object to store.
 */
export async function setup(_systemConfig, readline) {
  const question = (prompt) =>
    new Promise((resolve) => readline.question(prompt, resolve));

  console.log("\nStatus Module Setup");
  console.log("─────────────────────────────────────────────");
  console.log("Configure the hosts and network connections to monitor.\n");

  const hosts = [];

  while (true) {
    const id = await question("Host ID (e.g. 'machost') — leave blank to finish: ");
    if (!id.trim()) break;

    const name = await question("  Display name: ");
    const ip = await question("  IP address: ");
    const typeRaw = await question("  Type (local/ssh) [ssh]: ");
    const type = typeRaw.trim() === "local" ? "local" : "ssh";

    let user;
    if (type === "ssh") {
      user = await question("  SSH user: ");
    }

    hosts.push({
      id: id.trim(),
      name: name.trim() || id.trim(),
      ip: ip.trim(),
      type,
      ...(user && user.trim() ? { user: user.trim() } : {}),
    });

    const more = await question("Add another host? [y/N]: ");
    if (more.trim().toLowerCase() !== "y") break;
  }

  console.log("\nNetwork checks (ping targets):");
  const network = [];

  while (true) {
    const netId = await question("Network check ID (e.g. 'internet') — leave blank to finish: ");
    if (!netId.trim()) break;

    const netName = await question("  Display name: ");
    const netType = await question("  Type (internet/backup_internet/lan/router) [internet]: ");
    const target = await question("  Ping target (IP or hostname): ");

    network.push({
      id: netId.trim(),
      name: netName.trim() || netId.trim(),
      type: netType.trim() || "internet",
      target: target.trim() || null,
    });

    const more = await question("Add another network check? [y/N]: ");
    if (more.trim().toLowerCase() !== "y") break;
  }

  const sitesAnswer = await question('\nAuto-discover sites from nginx? (auto/none) [auto]: ');
  const sites = sitesAnswer.trim() === "none" ? [] : "auto";

  console.log("\nStatus module configured.");

  return {
    config: { hosts, network, sites },
  };
}
