import { defineHandler } from '@kb-labs/sdk';
import { loadLastRun } from '@kb-labs/qa-core';

// src/rest/handlers/details-handler.ts
var details_handler_default = defineHandler({
  async execute(ctx, _input) {
    const lastRun = loadLastRun(ctx.cwd);
    if (!lastRun) {
      return {
        timestamp: null,
        git: null,
        checks: {}
      };
    }
    const { results, packages, submodules, timestamp } = lastRun;
    const pkgMap = new Map(packages.map((p) => [p.name, p]));
    let git = null;
    try {
      const { execSync } = await import('child_process');
      const commit = execSync("git rev-parse --short HEAD", { cwd: ctx.cwd, encoding: "utf-8" }).trim();
      const branch = execSync("git rev-parse --abbrev-ref HEAD", { cwd: ctx.cwd, encoding: "utf-8" }).trim();
      const message = execSync("git log -1 --format=%s", { cwd: ctx.cwd, encoding: "utf-8" }).trim();
      git = { commit, branch, message };
    } catch {
    }
    const checks = {};
    for (const ct of Object.keys(results)) {
      const checkResult = results[ct];
      const passed = checkResult.passed.map((name) => {
        const pkg = pkgMap.get(name);
        return {
          name,
          repo: pkg?.repo ?? "unknown",
          status: "passed",
          submodule: pkg?.submodule
        };
      });
      const failed = checkResult.failed.map((name) => {
        const pkg = pkgMap.get(name);
        return {
          name,
          repo: pkg?.repo ?? "unknown",
          status: "failed",
          error: checkResult.errors[name],
          submodule: pkg?.submodule
        };
      });
      const skipped = checkResult.skipped.map((name) => {
        const pkg = pkgMap.get(name);
        return {
          name,
          repo: pkg?.repo ?? "unknown",
          status: "skipped",
          submodule: pkg?.submodule
        };
      });
      checks[ct] = { passed, failed, skipped };
    }
    return {
      timestamp,
      git,
      submodules,
      checks
    };
  }
});

export { details_handler_default as default };
//# sourceMappingURL=details-handler.js.map
//# sourceMappingURL=details-handler.js.map