import { performance } from "node:perf_hooks";
import { log } from "../utils/cli.ts";
import { installNodeDependencies } from "./installNodeDependencies.ts";
import { installPythonDependencies } from "./installPythonDependencies.ts";
import type { PrepDefinition, PrepOptions, PrepResult } from "./types.ts";

export type { PrepOptions, PrepResult } from "./types.ts";

// register all prep steps here
const prepSteps: PrepDefinition[] = [installNodeDependencies, installPythonDependencies];

/**
 * run all prep steps sequentially.
 * failures are logged as warnings but don't stop the run.
 */
export async function runPrepPhase(options: PrepOptions): Promise<PrepResult[]> {
  log.debug("» starting prep phase...");
  const startTime = performance.now();
  const results: PrepResult[] = [];

  for (const step of prepSteps) {
    const shouldRun = await step.shouldRun();
    if (!shouldRun) {
      log.debug(`» skipping ${step.name} (not applicable)`);
      continue;
    }

    log.debug(`» running ${step.name}...`);
    const result = await step.run(options);
    results.push(result);

    if (result.dependenciesInstalled) {
      log.debug(`» ${step.name}: dependencies installed`);
    } else if (result.issues.length > 0) {
      log.warning(`» ${step.name}: ${result.issues[0]}`);
    }
  }

  const totalDurationMs = performance.now() - startTime;
  log.debug(`» prep phase completed (${Math.round(totalDurationMs)}ms)`);

  return results;
}
