import { z } from 'zod';
import { specStore, knowledgeStore } from '../storage/index.js';
import { buildTeammatePrompt } from '../engine/team-planner/index.js';
import { readFile } from 'node:fs/promises';
export const generateTeammatePromptSchema = z.object({
    projectId: z.string().describe('Project identifier'),
    specId: z.string().describe('Spec ID to generate prompt for'),
    teammateName: z.string().describe('Name for the teammate'),
    assignedFiles: z.array(z.string()).describe('Files this teammate owns exclusively'),
    role: z
        .enum(['planner', 'implementer', 'reviewer', 'tester', 'researcher'])
        .describe('Role: planner | implementer | reviewer | tester | researcher'),
});
// ---------------------------------------------------------------------------
// 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 handleGenerateTeammatePrompt(params) {
    const { projectId, specId, teammateName, assignedFiles, role } = params;
    const spec = await specStore.getSpec(projectId, specId);
    if (!spec) {
        return err(`Spec '${specId}' not found in project '${projectId}'. Check the spec ID.`);
    }
    const knowledge = await knowledgeStore.getKnowledge(projectId);
    const constitution = await knowledgeStore.getConstitution(projectId);
    const criteria = await extractCriteria(spec.specPath);
    const prompt = buildTeammatePrompt({
        projectLanguage: knowledge?.language ?? 'unknown',
        projectFramework: knowledge?.framework ?? null,
        architecture: knowledge?.architecture.primary ?? 'unknown',
        conventions: knowledge?.conventions ?? {},
        specDetails: { id: spec.id, title: spec.title, criteria },
        ownedFiles: assignedFiles,
        forbiddenFiles: [],
        qualityRules: constitution?.principles.map((p) => p.principle) ?? [],
        testingRequirements: [
            `Test runner: ${knowledge?.testCommand ?? 'pnpm test'}`,
            'Coverage must be >= 95%',
            `Type check: ${knowledge?.buildCommand ?? 'pnpm typecheck'}`,
        ],
        gitConventions: [
            'Use conventional commits (feat:, fix:, docs:, etc.)',
            `Branch naming: feat/${spec.id}-short-name`,
            'Never commit directly to main or develop',
        ],
        validationCommands: [
            knowledge?.buildCommand ?? 'pnpm typecheck',
            knowledge?.lintCommand ?? 'pnpm lint',
            knowledge?.testCommand ?? 'pnpm test',
        ],
    });
    return text({ teammateName, role, specId, prompt });
}
//# sourceMappingURL=generate-teammate-prompt.js.map