const { runDoctor } = require("../lib/doctor");

module.exports = async function doctor(_args, flags) {
  const rootDir = process.cwd();
  const report = runDoctor(rootDir);

  if (flags.json) {
    console.log(JSON.stringify(report, null, 2));
    if (!report.pass) process.exit(report.exit_code);
    return;
  }

  console.log("\n## CapInt Doctor\n");
  console.log(`Result: ${report.pass ? "PASS" : "FAIL"}`);
  console.log(`Auto-fix: ${report.auto_fix ? "yes" : "no"}\n`);

  if (report.ok.length) {
    console.log("OK:");
    for (const o of report.ok) console.log(`  + ${o}`);
    console.log("");
  }
  if (report.warnings.length) {
    console.log("Warnings:");
    for (const w of report.warnings) console.log(`  ! ${w}`);
    console.log("");
  }
  if (report.issues.length) {
    console.log("Issues:");
    for (const i of report.issues) console.log(`  x ${i}`);
    console.log("");
  }
  console.log(report.hint);
  console.log("");

  if (!report.pass) process.exit(report.exit_code);
};
