// tools/team-analytics-handler.ts — Handlers for team_analytics and workload_distribution (SPEC-297)
import { listSpecs } from '../storage/spec-store.js';
import { hashProjectPath } from '../storage/base-store.js';
import { computeTeamAnalytics, computeWorkloadDistribution, } from '../engine/team-analytics-calculator.js';
import { formatSuccess, formatError, toolResult } from './response-helpers.js';
// ---------------------------------------------------------------------------
// team_analytics handler
// ---------------------------------------------------------------------------
export async function handleTeamAnalytics(args) {
    if (!args.projectPath || args.projectPath.trim().length === 0) {
        return toolResult(formatError('projectPath is required', 'Provide the absolute path to your project root.'), undefined, true);
    }
    const projectId = hashProjectPath(args.projectPath);
    const weeks = Math.min(Math.max(args.weeks ?? 4, 1), 52);
    const specs = await listSpecs(projectId);
    const result = computeTeamAnalytics(specs, weeks);
    const lines = [
        `Team Analytics — ${args.projectPath}`,
        `Period: last ${String(weeks)} weeks | Total specs: ${String(result.teamSummary.totalSpecs)}`,
        `Avg velocity: ${result.teamSummary.avgVelocity.toFixed(1)} specs/week | Top contributor: ${result.teamSummary.topContributor}`,
        '',
        '**Per-member metrics:**',
    ];
    for (const m of result.members) {
        lines.push(`  ${m.name}:`);
        lines.push(`    Completed: ${String(m.specsCompleted)} | WIP: ${String(m.currentWip)}`);
        lines.push(`    Avg lead time: ${m.avgLeadTimeDays.toFixed(1)} days | Completion rate: ${(m.completionRate * 100).toFixed(0)}%`);
        lines.push(`    Estimated hours: ${m.totalEstimatedHours.toFixed(0)}h`);
    }
    if (result.members.length === 0) {
        lines.push('  No specs found for this project.');
    }
    const body = lines.join('\n');
    return toolResult(formatSuccess('Team Analytics', body), result);
}
// ---------------------------------------------------------------------------
// workload_distribution handler
// ---------------------------------------------------------------------------
export async function handleWorkloadDistribution(args) {
    if (!args.projectPath || args.projectPath.trim().length === 0) {
        return toolResult(formatError('projectPath is required', 'Provide the absolute path to your project root.'), undefined, true);
    }
    const projectId = hashProjectPath(args.projectPath);
    const specs = await listSpecs(projectId);
    const result = computeWorkloadDistribution(specs);
    const lines = [
        `Workload Distribution — ${args.projectPath}`,
        '',
        '**Current workload per member:**',
    ];
    for (const m of result.members) {
        lines.push(`  ${m.name}: WIP=${String(m.wip)} | Backlog=${String(m.backlog)} | Est. hours=${m.estimatedHours.toFixed(0)}h`);
    }
    if (result.members.length === 0) {
        lines.push('  No specs found for this project.');
    }
    if (result.imbalanceWarnings.length > 0) {
        lines.push('');
        lines.push('**Imbalance warnings:**');
        for (const w of result.imbalanceWarnings) {
            lines.push(`  - ${w.message}`);
        }
    }
    const body = lines.join('\n');
    return toolResult(formatSuccess('Workload Distribution', body), result);
}
//# sourceMappingURL=team-analytics-handler.js.map