import { defineCommand } from '@kb-labs/sdk';
import { CACHE_KEYS } from '@kb-labs/quality-contracts';
import { checkBuilds } from '@kb-labs/quality-core/builds';

// src/cli/commands/check-builds.ts
var check_builds_default = defineCommand({
  id: "quality:check-builds",
  description: "Check build status across monorepo",
  handler: {
    async execute(ctx, input) {
      const { ui, platform } = ctx;
      const flags = input.flags ?? input;
      const cacheKey = `${CACHE_KEYS.BUILDS}:${flags.package || "all"}`;
      if (!flags.refresh) {
        const cached = await platform.cache.get(cacheKey);
        if (cached) {
          outputBuildCheck({ ...cached, cached: true }, flags, ui);
          return { exitCode: cached.failing > 0 ? 1 : 0, result: cached };
        }
      }
      const result = await checkBuilds(ctx.cwd, {
        packageFilter: flags.package,
        timeout: flags.timeout ? Number(flags.timeout) : 3e4
      });
      await platform.cache.set(cacheKey, result, 10 * 60 * 1e3);
      await platform.analytics.track("quality:check-builds", {
        totalPackages: result.totalPackages,
        passing: result.passing,
        failing: result.failing,
        staleBuilds: result.staleBuilds.length,
        duration: result.duration,
        packageSpecific: !!flags.package
      });
      outputBuildCheck({ ...result, cached: false }, flags, ui);
      return {
        exitCode: result.failing > 0 ? 1 : 0,
        result
      };
    }
  }
});
function outputBuildCheck(result, flags, ui) {
  if (flags.json) {
    ui?.json?.(result);
    return;
  }
  const sections = [];
  const statusIcon = result.failing === 0 ? "\u2705" : "\u274C";
  const statusText = result.failing === 0 ? "All builds passing" : "Build failures detected";
  const statusItems = [
    `${statusIcon} ${statusText}`,
    `Passing: ${result.passing} package(s)`,
    result.failing > 0 ? `Failing: ${result.failing} package(s)` : null
  ].filter(Boolean);
  sections.push({ header: "Build Status", items: statusItems });
  if (result.failures.length > 0) {
    const failureItems = [];
    for (const failure of result.failures.slice(0, 5)) {
      failureItems.push(`\u2022 ${failure.package}`);
      const firstLine = failure.error.split("\n")[0];
      failureItems.push(`  ${firstLine?.substring(0, 80) || "Build failed"}`);
    }
    if (result.failures.length > 5) {
      failureItems.push(`... and ${result.failures.length - 5} more failures`);
    }
    sections.push({ header: "\u274C Failed Packages", items: failureItems });
  }
  if (result.staleBuilds.length > 0) {
    const staleItems = result.staleBuilds.slice(0, 5).map((s) => `\u2022 ${s.package}`);
    if (result.staleBuilds.length > 5) {
      staleItems.push(`... and ${result.staleBuilds.length - 5} more`);
    }
    sections.push({
      header: "\u26A0\uFE0F  Stale Builds (dist/ older than src/)",
      items: staleItems
    });
  }
  const summaryItems = [
    `Total packages: ${result.totalPackages}`,
    `Duration: ${(result.duration / 1e3).toFixed(1)}s`,
    result.cached ? "\u{1F4BE} Cached (use --refresh to recheck)" : "\u{1F504} Fresh check"
  ];
  sections.push({ header: "Summary", items: summaryItems });
  const title = result.failing === 0 ? "\u2705 All Builds Passing" : `\u274C ${result.failing} Build Failure(s) Detected`;
  ui?.success?.("Build check completed", {
    title,
    sections
  });
}

export { check_builds_default as default };
//# sourceMappingURL=check-builds.js.map
//# sourceMappingURL=check-builds.js.map