import { type CommandResult, success, silentFailure } from "../../lib/command-result";
import { formatSyncCheckReport } from "../../lib/sync-check/format";
import { emptyResult, mergeResults, type SyncCheckResult } from "../../lib/sync-check/shared";
import { runSourceSyncCheck } from "../../lib/sync-check/source";
import { moduleCategories, moduleModelCategory } from "./categories";
import { runModelSyncCheck } from "./models";
import { runModuleTestCaseSyncCheck } from "./module-test-cases";

export async function collectModuleSyncResult(
  modulesRoot: string,
  cwd: string,
): Promise<SyncCheckResult> {
  const result = emptyResult();
  mergeResults(result, await runSourceSyncCheck(moduleCategories(modulesRoot), cwd));
  mergeResults(result, await runModelSyncCheck(moduleModelCategory(modulesRoot), cwd));
  mergeResults(result, await runModuleTestCaseSyncCheck(modulesRoot, cwd));
  return result;
}

export async function runModuleSyncCheck(modulesRoot: string, cwd: string): Promise<CommandResult> {
  const result = await collectModuleSyncResult(modulesRoot, cwd);
  console.log(formatSyncCheckReport(result));
  return result.errors.length === 0 && result.filesChecked > 0 ? success() : silentFailure(1);
}
