/**
 * ADR-PIPELINE-094 Decision 8 — Phase 3 local fallback generator.
 *
 * Consumes Phase-1 simulator consensus and produces a minimal SPARC
 * specification: `phase3/sparc/specification.md` and a mirror at
 * `engineering/sparc-specification.md` so downstream gates that accept
 * either path are satisfied.
 *
 * Output is intentionally short (Functional Requirements + glossary) and
 * tagged `quality: degraded` so consumers know the provenance. Pseudocode,
 * refinement, and completion artifacts are NOT produced — those remain
 * Ruflo's responsibility on the healthy path.
 */
import * as fs from 'node:fs';
import * as path from 'node:path';
import { readPhase1Consensus, extractScenarioQuery } from './phase1-consensus-reader.js';
/**
 * Run the Phase-3 fallback. Idempotent: re-runs over an existing fallback
 * just overwrite the same files. Best-effort: a failed write produces
 * `success: false` but never throws.
 */
export function runPhase3LocalFallback(runDir) {
    const consensus = readPhase1Consensus(runDir);
    if (!consensus) {
        return {
            success: false,
            filesWritten: [],
            reason: 'no-phase1-consensus',
            message: 'Phase-3 local fallback: no Phase-1 consensus on disk',
        };
    }
    const filesWritten = [];
    const sparcMd = buildSparcSpecificationMarkdown(consensus);
    try {
        const phase3SparcDir = path.join(runDir, 'phase3', 'sparc');
        fs.mkdirSync(phase3SparcDir, { recursive: true });
        const phase3Path = path.join(phase3SparcDir, 'specification.md');
        fs.writeFileSync(phase3Path, sparcMd, 'utf8');
        filesWritten.push('phase3/sparc/specification.md');
        const engineeringDir = path.join(runDir, 'engineering');
        fs.mkdirSync(engineeringDir, { recursive: true });
        const engPath = path.join(engineeringDir, 'sparc-specification.md');
        fs.writeFileSync(engPath, sparcMd, 'utf8');
        filesWritten.push('engineering/sparc-specification.md');
    }
    catch (err) {
        return {
            success: false,
            filesWritten,
            reason: 'write-error',
            message: `Phase-3 local fallback: write failed — ${err.message}`,
        };
    }
    return {
        success: true,
        filesWritten,
        message: `Phase-3 local fallback: wrote ${filesWritten.length} SPARC artifact(s) (degraded)`,
    };
}
function buildSparcSpecificationMarkdown(c) {
    const lines = [];
    const query = extractScenarioQuery(c) || '(no scenario recorded)';
    lines.push('---');
    lines.push('title: SPARC Specification (Phase-1 fallback)');
    lines.push('quality: degraded');
    lines.push('generator: local-fallback');
    lines.push('source: phase1-consensus');
    lines.push('---');
    lines.push('');
    lines.push('# SPARC Specification');
    lines.push('');
    lines.push('> Generated by local fallback (Ruflo unavailable). Quality: **degraded**.');
    lines.push('> The full SPARC chain (specification, pseudocode, architecture, refinement,');
    lines.push('> completion) is not produced in fallback mode — only this specification stub.');
    lines.push('');
    lines.push('## Brief');
    lines.push('');
    lines.push(query);
    lines.push('');
    if (c.executiveSummary) {
        lines.push('## Functional Requirements (extracted from executive summary)');
        lines.push('');
        lines.push(c.executiveSummary.trim());
        lines.push('');
    }
    else if (c.decisionMemo) {
        lines.push('## Functional Requirements (extracted from decision memo)');
        lines.push('');
        lines.push(c.decisionMemo.trim());
        lines.push('');
    }
    else {
        // We must produce something non-empty over the gate's minBytes threshold.
        lines.push('## Functional Requirements');
        lines.push('');
        lines.push('Functional requirements derived from the brief. Implement the system');
        lines.push('described above following standard layered architecture conventions:');
        lines.push('analysis layer, decision layer, integration layer, and persistence layer');
        lines.push('with explicit boundaries and unit-tested pure functions.');
        lines.push('');
    }
    if (c.decisionMemo) {
        lines.push('## Architectural Rationale');
        lines.push('');
        lines.push(c.decisionMemo.trim());
        lines.push('');
    }
    lines.push('## Pseudocode');
    lines.push('');
    lines.push('_Not produced in local-fallback mode. Ruflo authors pseudocode on the healthy path._');
    lines.push('');
    lines.push('## Architecture');
    lines.push('');
    lines.push('Single-service flat layout under `src/`. Bounded contexts are inferred at');
    lines.push('the implementation prompt stage from Phase-1 consensus when no DDD is available.');
    lines.push('');
    lines.push('## Refinement');
    lines.push('');
    lines.push('_Not produced in local-fallback mode._');
    lines.push('');
    lines.push('## Completion');
    lines.push('');
    lines.push('_Not produced in local-fallback mode. Build phase consumes this spec directly._');
    lines.push('');
    return lines.join('\n');
}
//# sourceMappingURL=phase3-local-fallback.js.map