import { listSpecs } from '../storage/spec-store.js';
import { hashProjectPath } from '../storage/base-store.js';
import { simulateParallelExecution, analyzeCriticalPath, computeParallelEfficiency, optimizeSpecScheduling, } from '../engine/spec-scheduler.js';
import { formatSuccess, formatError, toolResult } from './response-helpers.js';
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
async function getSpecs(projectPath, specIds) {
    const projectId = hashProjectPath(projectPath);
    const all = await listSpecs(projectId);
    if (!specIds || specIds.length === 0) {
        return all;
    }
    return all.filter((s) => specIds.includes(s.id));
}
// ---------------------------------------------------------------------------
// simulate_parallel_execution
// ---------------------------------------------------------------------------
export async function handleSimulateParallelExecution(args) {
    if (!args.projectPath.trim()) {
        return toolResult(formatError('projectPath is required'), undefined, true);
    }
    if (!Array.isArray(args.specIds) || args.specIds.length === 0) {
        return toolResult(formatError('specIds must be a non-empty array'), undefined, true);
    }
    const specs = await getSpecs(args.projectPath, args.specIds);
    if (specs.length === 0) {
        return toolResult(formatError('No matching specs found'), undefined, true);
    }
    const numAgents = Math.max(1, Math.min(args.numAgents, 20));
    const timeline = simulateParallelExecution(specs, numAgents);
    const lines = [
        `Specs: ${String(specs.length)} | Agents: ${String(numAgents)}`,
        `Total hours: ${timeline.totalHours.toFixed(1)}h`,
        `Critical path: ${timeline.criticalPath.join(' → ') || 'none'}`,
        '',
        timeline.ganttChart,
    ];
    return toolResult(formatSuccess('Parallel Execution Simulation', lines.join('\n')), timeline);
}
// ---------------------------------------------------------------------------
// critical_path_analyzer
// ---------------------------------------------------------------------------
export async function handleCriticalPathAnalyzer(args) {
    if (!args.projectPath.trim()) {
        return toolResult(formatError('projectPath is required'), undefined, true);
    }
    const specs = await getSpecs(args.projectPath, args.specIds);
    if (specs.length === 0) {
        return toolResult(formatError('No specs found'), undefined, true);
    }
    const result = analyzeCriticalPath(specs);
    const lines = [
        `Total specs analyzed: ${String(specs.length)}`,
        `Total estimated hours: ${result.totalHours.toFixed(1)}h`,
        `Critical path length: ${String(result.longestChainLength)} specs`,
        `Critical path: ${result.criticalPath.join(' → ') || 'empty'}`,
    ];
    return toolResult(formatSuccess('Critical Path Analysis', lines.join('\n')), result);
}
// ---------------------------------------------------------------------------
// parallel_efficiency_score
// ---------------------------------------------------------------------------
export async function handleParallelEfficiencyScore(args) {
    if (!args.projectPath.trim()) {
        return toolResult(formatError('projectPath is required'), undefined, true);
    }
    if (!Array.isArray(args.specIds) || args.specIds.length === 0) {
        return toolResult(formatError('specIds must be a non-empty array'), undefined, true);
    }
    const specs = await getSpecs(args.projectPath, args.specIds);
    if (specs.length === 0) {
        return toolResult(formatError('No matching specs found'), undefined, true);
    }
    const numAgents = Math.max(1, Math.min(args.numAgents, 20));
    const result = computeParallelEfficiency(specs, numAgents);
    const lines = [
        `Efficiency score: ${(result.score * 100).toFixed(1)}%`,
        `Total spec hours: ${result.totalSpecHours.toFixed(1)}h`,
        `Total agent-hours: ${result.totalAgentHours.toFixed(1)}h`,
        `Interpretation: ${result.interpretation}`,
    ];
    return toolResult(formatSuccess('Parallel Efficiency Score', lines.join('\n')), result);
}
// ---------------------------------------------------------------------------
// optimize_spec_scheduling
// ---------------------------------------------------------------------------
export async function handleOptimizeSpecScheduling(args) {
    if (!args.projectPath.trim()) {
        return toolResult(formatError('projectPath is required'), undefined, true);
    }
    const specs = await getSpecs(args.projectPath, args.specIds);
    if (specs.length === 0) {
        return toolResult(formatError('No specs found'), undefined, true);
    }
    const numAgents = Math.max(1, Math.min(args.numAgents ?? 2, 20));
    const result = optimizeSpecScheduling(specs, numAgents);
    const lines = [
        `Optimized order (${String(result.order.length)} specs):`,
        ...result.order.slice(0, 10).map((id, i) => `  ${String(i + 1)}. ${id}`),
        result.order.length > 10 ? `  ... and ${String(result.order.length - 10)} more` : '',
        '',
        `Expected idle hours: ${result.expectedIdleHours.toFixed(1)}h`,
        `Suggestion: ${result.suggestion}`,
    ].filter((l) => l !== '');
    return toolResult(formatSuccess('Optimized Scheduling', lines.join('\n')), result);
}
//# sourceMappingURL=spec-scheduler-handler.js.map