import { appendArtifact } from '../handoff-artifacts/io.js';
import { runSpecCompliance, } from './spec-compliance-runner.js';
export async function writeImplementationReviewReport(input) {
    const specCompliance = await runSpecCompliance(input.spec, input.projectPath);
    const gates = buildGates({
        score: input.score,
        lintPassed: input.lintPassed ?? true,
        conventionRegression: input.conventionRegression ?? false,
        specCompliance,
    });
    const passed = gates.every((gate) => gate.passed);
    const reviewer = {
        kind: 'implementation-review-agent',
        agent: 'planu-implementation-reviewer',
        verdict: passed ? 'approved' : 'changes-requested',
    };
    const reportCompliance = toValidationReportCompliance(specCompliance);
    const payload = {
        schema_version: '1.0.0',
        specId: input.specId,
        passed,
        gates,
        reviewer,
        specCompliance: reportCompliance,
        score: input.score ?? undefined,
        completedAt: new Date().toISOString(),
    };
    try {
        const artifact = await appendArtifact({
            projectId: input.projectId,
            specId: input.specId,
            kind: 'validation-report',
            payload,
        });
        return {
            written: true,
            path: artifact.path,
            sha: artifact.sha,
            reviewer,
            passed,
            gates,
            specCompliance: reportCompliance,
        };
    }
    catch (err) {
        return {
            written: false,
            error: err instanceof Error ? err.message : String(err),
            reviewer,
            passed,
            gates,
            specCompliance: reportCompliance,
        };
    }
}
function buildGates(args) {
    return [
        {
            name: 'validate-score',
            passed: args.score === 100,
            reason: args.score === 100
                ? undefined
                : `Expected validation score 100 before done, got ${String(args.score ?? 'unscored')}.`,
        },
        {
            name: 'lint',
            passed: args.lintPassed,
            reason: args.lintPassed ? undefined : 'Configured lint command failed.',
        },
        {
            name: 'convention-regression',
            passed: !args.conventionRegression,
            reason: args.conventionRegression ? 'Convention baseline regression detected.' : undefined,
        },
        buildSpecComplianceGate(args.specCompliance),
    ];
}
function buildSpecComplianceGate(result) {
    if (result.perScenario.length === 0) {
        return {
            name: 'spec-compliance',
            passed: true,
            reason: 'No executable scenarios declared; spec-compliance runner skipped.',
        };
    }
    const passed = result.dimensionScore === 100;
    return {
        name: 'spec-compliance',
        passed,
        reason: passed
            ? undefined
            : `Expected all executable scenarios to pass, got ${String(result.dimensionScore)}.`,
    };
}
function toValidationReportCompliance(result) {
    return {
        score: result.dimensionScore,
        command: result.command,
        scenarios: result.perScenario.map((scenario) => ({
            title: scenario.title,
            verdict: scenario.verdict,
            evidence: scenario.evidence,
        })),
    };
}
//# sourceMappingURL=validation-report-writer.js.map