import { spawnSync } from "node:child_process";

const batches = [
  {
    name: "allocation-and-quick-assign",
    files: [
      "src/__tests__/assistant-tools-allocation-create-success.test.ts",
      "src/__tests__/assistant-tools-allocation-create-errors.test.ts",
      "src/__tests__/assistant-tools-allocation-status-success.test.ts",
      "src/__tests__/assistant-tools-allocation-status-errors.test.ts",
      "src/__tests__/assistant-tools-allocation-cancel-success.test.ts",
      "src/__tests__/assistant-tools-allocation-cancel-errors.test.ts",
      "src/__tests__/assistant-tools-timeline-quick-assign-success.test.ts",
      "src/__tests__/assistant-tools-timeline-quick-assign-errors.test.ts",
      "src/__tests__/assistant-tools-timeline-batch-quick-assign-success.test.ts",
      "src/__tests__/assistant-tools-timeline-batch-quick-assign-errors.test.ts",
    ],
  },
  {
    name: "timeline-and-advanced-ranking",
    files: [
      "src/__tests__/assistant-tools-timeline-allocation-shifts.test.ts",
      "src/__tests__/assistant-tools-timeline-allocation-shift-errors.test.ts",
      "src/__tests__/assistant-tools-timeline-inline-allocation-update-success.test.ts",
      "src/__tests__/assistant-tools-timeline-inline-allocation-update-errors.test.ts",
      "src/__tests__/assistant-tools-timeline-project-shifts.test.ts",
      "src/__tests__/assistant-tools-timeline-resource-selection.test.ts",
      "src/__tests__/assistant-tools-advanced-resource-ranking.test.ts",
      "src/__tests__/assistant-tools-advanced-timeline-entries-view.test.ts",
      "src/__tests__/assistant-tools-advanced-timeline-holiday-overlays.test.ts",
      "src/__tests__/assistant-tools-advanced-project-timeline-context.test.ts",
      "src/__tests__/assistant-tools-advanced-project-shift-preview.test.ts",
    ],
  },
  {
    name: "dispo",
    files: [
      "src/__tests__/assistant-tools-dispo-staged-listings-resources-projects.test.ts",
      "src/__tests__/assistant-tools-dispo-staged-unresolved-read.test.ts",
      "src/__tests__/assistant-tools-dispo-staged-vacations-read.test.ts",
      "src/__tests__/assistant-tools-dispo-staged-assignments-read.test.ts",
      "src/__tests__/assistant-tools-dispo-staged-resolution.test.ts",
      "src/__tests__/assistant-tools-dispo-import.test.ts",
      "src/__tests__/assistant-tools-dispo-import-batch-list-cancel.test.ts",
      "src/__tests__/assistant-tools-dispo-import-batch-delegation.test.ts",
    ],
  },
  {
    name: "estimate",
    files: [
      "src/__tests__/assistant-tools-estimate-read-detail-access.test.ts",
      "src/__tests__/assistant-tools-estimate-read-not-found.test.ts",
      "src/__tests__/assistant-tools-estimate-read-version-snapshot-access.test.ts",
      "src/__tests__/assistant-tools-estimate-read-versions-list-access.test.ts",
      "src/__tests__/assistant-tools-estimate-clone-errors.test.ts",
      "src/__tests__/assistant-tools-estimate-commercial-terms-errors.test.ts",
      "src/__tests__/assistant-tools-estimate-creation-races.test.ts",
      "src/__tests__/assistant-tools-estimate-draft-errors.test.ts",
      "src/__tests__/assistant-tools-estimate-generate-weekly-phasing-errors.test.ts",
      "src/__tests__/assistant-tools-estimate-get-weekly-phasing-errors.test.ts",
      "src/__tests__/assistant-tools-estimate-planning-handoff-errors.test.ts",
      "src/__tests__/assistant-tools-estimate-revision-export-errors.test.ts",
      "src/__tests__/assistant-tools-estimate-version-status-errors.test.ts",
    ],
  },
  {
    name: "insights-and-misc",
    files: [
      "src/__tests__/assistant-tools-insights-summary.test.ts",
      "src/__tests__/assistant-tools-insights-anomalies.test.ts",
      "src/__tests__/assistant-tools-scenarios.test.ts",
      "src/__tests__/assistant-tools-budget-status.test.ts",
      "src/__tests__/assistant-tools-chargeability-report.test.ts",
      "src/__tests__/assistant-tools-auth-guard.test.ts",
      "src/__tests__/assistant-tools-master-data-rate-lookup.test.ts",
      "src/__tests__/assistant-tools-query-change-history.test.ts",
      "src/__tests__/assistant-tools-project-cover-generate.test.ts",
      "src/__tests__/assistant-tools-project-cover-remove.test.ts",
      "src/__tests__/assistant-tools-project-narrative.test.ts",
      "src/__tests__/assistant-tools-project-computation-graph.test.ts",
      "src/__tests__/assistant-tools-resource-computation-graph.test.ts",
      "src/__tests__/assistant-tools-settings-role-config-admin.test.ts",
      "src/__tests__/assistant-tools-import.test.ts",
      "src/__tests__/assistant-tools-export.test.ts",
      "src/__tests__/assistant-tools-export-projects.test.ts",
      "src/__tests__/assistant-tools-holiday-resolution-errors.test.ts",
      "src/__tests__/ai-client.test.ts",
    ],
  },
];

function run(command, args) {
  const result = spawnSync(command, args, {
    cwd: new URL("..", import.meta.url),
    stdio: "inherit",
    shell: process.platform === "win32",
  });
  if (result.status !== 0) {
    process.exit(result.status ?? 1);
  }
}

for (const batch of batches) {
  console.log(`\n== assistant split regression: ${batch.name} ==`);
  run("pnpm", ["exec", "vitest", "run", ...batch.files]);
}

console.log("\n== assistant split regression: typecheck ==");
run("pnpm", ["exec", "tsc", "-p", "tsconfig.json", "--noEmit", "--pretty", "false"]);
