// tools/package-handoff.ts — Handoff context packager tool (SPEC-039)
import { specStore, knowledgeStore } from '../storage/index.js';
import { checkSpecReadiness } from '../engine/readiness-checker.js';
import { packageHandoff } from '../engine/handoff-packager.js';
import { detectParadigms } from '../engine/paradigm-detector.js';
// ── Formatting helpers ───────────────────────────────────────────────────────
function formatHandoff(pkg) {
    const lines = [];
    lines.push(`# Implementation Handoff Package — ${pkg.specId}`);
    lines.push('');
    lines.push(`Generated: ${pkg.generatedAt}`);
    lines.push(`Readiness Score: ${pkg.readinessScore}/100`);
    lines.push('');
    lines.push('## Objective');
    lines.push('');
    lines.push(pkg.objective);
    lines.push('');
    lines.push('## Acceptance Criteria');
    lines.push('');
    if (pkg.criteria.length > 0) {
        for (const criterion of pkg.criteria) {
            lines.push(`- [ ] ${criterion}`);
        }
    }
    else {
        lines.push('_(No criteria found — check HU.md)_');
    }
    lines.push('');
    lines.push('## Files to Modify');
    lines.push('');
    if (pkg.filesToModify.length > 0) {
        for (const f of pkg.filesToModify) {
            lines.push(`- ${f}`);
        }
    }
    else {
        lines.push('_(No files identified — check FICHA-TECNICA.md)_');
    }
    lines.push('');
    lines.push('## Files to Create');
    lines.push('');
    if (pkg.filesToCreate.length > 0) {
        for (const f of pkg.filesToCreate) {
            lines.push(`- ${f}`);
        }
    }
    else {
        lines.push('_(None identified)_');
    }
    lines.push('');
    lines.push('## OUT OF SCOPE');
    lines.push('');
    lines.push('**The following must NOT be implemented:**');
    lines.push('');
    for (const item of pkg.outOfScope) {
        lines.push(`- ${item}`);
    }
    lines.push('');
    if (pkg.constraints.length > 0) {
        lines.push('## Hard Constraints');
        lines.push('');
        for (const constraint of pkg.constraints) {
            lines.push(`- [${constraint.type.toUpperCase()}] ${constraint.description}`);
            if (constraint.affectedItems.length > 0) {
                lines.push(`  Affects: ${constraint.affectedItems.join(', ')}`);
            }
        }
        lines.push('');
    }
    if (pkg.warnings.length > 0) {
        lines.push('## Warnings');
        lines.push('');
        for (const warning of pkg.warnings) {
            lines.push(`- WARNING: ${warning}`);
        }
        lines.push('');
    }
    return lines.join('\n');
}
// ── Handler ──────────────────────────────────────────────────────────────────
export async function handlePackageHandoff(args) {
    const { projectId, specId } = args;
    const spec = await specStore.getSpec(projectId, specId);
    if (!spec) {
        return {
            content: [
                {
                    type: 'text',
                    text: `Spec '${specId}' not found in project '${projectId}'`,
                },
            ],
            isError: true,
        };
    }
    const knowledge = await knowledgeStore.getKnowledge(projectId);
    if (!knowledge) {
        return {
            content: [
                {
                    type: 'text',
                    text: `Project '${projectId}' not found. Run init_project first.`,
                },
            ],
            isError: true,
        };
    }
    // Run readiness check to populate score
    const readinessReport = await checkSpecReadiness(spec, 'lenient');
    const pkg = await packageHandoff(spec, knowledge);
    // SPEC-035b: Inject paradigm constraint when project path is available
    const paradigmConstraints = pkg.constraints.slice();
    if (knowledge.projectPath) {
        try {
            const paradigmReport = await detectParadigms(knowledge.projectPath);
            const dominant = paradigmReport.dominant;
            if (dominant && dominant.confidence >= 0.5) {
                paradigmConstraints.push({
                    type: 'interface',
                    description: `Follow ${dominant.name} paradigm patterns — dominant paradigm (confidence: ${(dominant.confidence * 100).toFixed(0)}%)`,
                    affectedItems: ['all new code'],
                });
            }
        }
        catch {
            // Paradigm detection is best-effort — never block handoff
        }
    }
    // Inject readiness score
    const pkgWithScore = {
        ...pkg,
        readinessScore: readinessReport.score,
        constraints: paradigmConstraints,
    };
    const formatted = formatHandoff(pkgWithScore);
    return {
        content: [{ type: 'text', text: formatted }],
    };
}
//# sourceMappingURL=package-handoff.js.map