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 { runActorSyncCheck } from "./actors";
import { runAppTestCaseSyncCheck } from "./app-test-cases";
import { appComposeCategories } from "./categories";
import { runE2eSyncCheck } from "./e2e-flows";
import { runScreenPageSyncCheck } from "./screen-pages";
import { runSpecFixtureSyncCheck } from "./spec-fixtures";

async function collectAppSyncResult(appRoot: string, cwd: string): Promise<SyncCheckResult> {
  const result = emptyResult();
  mergeResults(result, await runSourceSyncCheck(appComposeCategories(appRoot), cwd));
  mergeResults(result, await runAppTestCaseSyncCheck(appRoot, cwd));
  mergeResults(result, await runE2eSyncCheck(appRoot, cwd));
  mergeResults(result, await runActorSyncCheck(appRoot, cwd));
  mergeResults(result, await runScreenPageSyncCheck(appRoot, cwd));
  mergeResults(result, await runSpecFixtureSyncCheck(appRoot, cwd));
  return result;
}

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