import { resolve } from "node:path";

import { exportDemoPackage } from "./export-package.ts";
import { generateProofEvaluation } from "./evaluate.ts";
import { generateDemoHub } from "./demo-hub.ts";
import { loadCaseFile } from "./intake.ts";
import { runCase } from "./run.ts";
import { setupCrwaTarget, stopCrwaTarget } from "./targets/crwa-lifecycle.ts";

const DEMO_CASES = [
  "cases/cypress-realworld-mobile-date-range-drawer.case.json",
  "cases/cypress-realworld-mobile-onboarding-dialog.case.json",
] as const;

export type DemoRunResult = {
  cases: Array<{
    id: string;
    artifactDirectory: string;
    scorecardPath: string;
  }>;
  evaluationPath: string;
  exportPath: string;
  cockpitPath: string;
};

export async function runDemo(): Promise<DemoRunResult> {
  await setupCrwaTarget();
  await stopCrwaTarget({ quiet: true });

  const cases = [];
  for (const casePath of DEMO_CASES) {
    const caseFile = await loadCaseFile(casePath);
    const artifactDirectory = await runCase(caseFile);
    cases.push({
      id: caseFile.id,
      artifactDirectory,
      scorecardPath: resolve(caseFile.artifacts.directory, "scorecard.json"),
    });
  }

  const primaryCase = await loadCaseFile(DEMO_CASES[0]);
  const evaluationPath = await generateProofEvaluation({
    manifestPath: "evals/proof-benchmark.json",
  });
  const cockpitPath = await generateDemoHub(primaryCase);
  const exportPath = await exportDemoPackage(primaryCase);

  return {
    cases,
    evaluationPath,
    exportPath,
    cockpitPath,
  };
}
