import { z } from 'zod';
import { specStore } from '../storage/index.js';
import { validateTeamResults } from '../engine/team-planner/index.js';
import { readFile } from 'node:fs/promises';
export const validateTeamResultsSchema = z.object({
    projectId: z.string().describe('Project identifier'),
    specIds: z.array(z.string()).describe('Spec IDs that were distributed across the team'),
    teammateNames: z.array(z.string()).describe('Names of teammates whose results to validate'),
});
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function text(content) {
    return { content: [{ type: 'text', text: JSON.stringify(content, null, 2) }] };
}
function err(message) {
    return { content: [{ type: 'text', text: message }], isError: true };
}
/** Extract acceptance criteria from a spec.md file. */
async function extractCriteria(specPath) {
    try {
        const content = await readFile(specPath, 'utf-8');
        const criteria = [];
        let inCriteria = false;
        for (const line of content.split('\n')) {
            if (/acceptance\s+criteria/i.test(line) || /criterios?\s+de\s+aceptaci/i.test(line)) {
                inCriteria = true;
                continue;
            }
            if (inCriteria && /^#{1,3}\s/.test(line) && !/acceptance|criterio/i.test(line)) {
                break;
            }
            if (inCriteria && /^\s*[-*]\s*\[.\]/.test(line)) {
                criteria.push(line.replace(/^\s*[-*]\s*\[.\]\s*/, '').trim());
            }
        }
        return criteria;
    }
    catch {
        return [];
    }
}
// ---------------------------------------------------------------------------
// Handler
// ---------------------------------------------------------------------------
export async function handleValidateTeamResults(params) {
    const { projectId, specIds, teammateNames } = params;
    if (specIds.length === 0) {
        return err('specIds must contain at least one spec ID. Provide the specs that were worked on.');
    }
    // Load expected criteria per spec
    const expectedCriteria = {};
    for (const specId of specIds) {
        const spec = await specStore.getSpec(projectId, specId);
        if (!spec) {
            return err(`Spec '${specId}' not found in project '${projectId}'. Check the spec ID.`);
        }
        expectedCriteria[specId] = await extractCriteria(spec.specPath);
    }
    // Build empty teammate outputs — the tool provides the validation framework.
    // Actual file modifications and criteria coverage are reported by teammates
    // and cannot be inspected from within MCP. The LLM fills in the details.
    const teammateOutputs = teammateNames.map((name) => ({
        name,
        modifiedFiles: [],
        coveredCriteria: [],
    }));
    const result = validateTeamResults(teammateOutputs, expectedCriteria, {});
    return text({
        ...result,
        instructions: [
            'Run these commands to complete validation:',
            '  pnpm typecheck',
            '  pnpm lint',
            '  pnpm test:coverage',
        ].join('\n'),
        nextSteps: result.passed
            ? ['All structural validations passed. Run CI commands above, then merge.']
            : ['Fix reported issues before merging. Then run CI commands above.'],
    });
}
//# sourceMappingURL=validate-team-results.js.map