import { defineCommand } from "politty";
import { executeCommand, success, silentFailure } from "../../lib/command-result";
import { formatGhActionsSyncReport, runGhActionsSyncCheck } from "./sync-check";

const cwd = process.cwd();

const syncCheckCommand = defineCommand({
  name: "sync-check",
  description:
    "Detect drift between local .github/workflows + .github/actions and erp-kit templates",
  run: () => {
    return executeCommand(async () => {
      const result = await runGhActionsSyncCheck(cwd);
      console.log(formatGhActionsSyncReport(result));
      return result.exitCode === 0 ? success() : silentFailure(result.exitCode);
    });
  },
});

export const ghActionsCommand = defineCommand({
  name: "gh-actions",
  description: "GitHub Actions template management",
  subCommands: {
    "sync-check": syncCheckCommand,
  },
});
