/**
 * Stage artifact scaffolding for RCASD lifecycle stages.
 *
 * Ensures each stage has a canonical markdown artifact under:
 *   .cleo/rcasd/{epicId}/{stage-subdir}/{epicId}-{stage}.md
 *
 * Frontmatter is maintained with related links so backlinks can be
 * discovered via frontmatter graph traversal.
 *
 * @task T5217
 */
import { existsSync } from 'node:fs';
import { readFile, writeFile } from 'node:fs/promises';
import { dirname, join, relative } from 'node:path';
import { getProjectRoot } from '../paths.js';
import { addFrontmatter, buildFrontmatter } from './frontmatter.js';
import { ensureStagePath, getStagePath } from './rcasd-paths.js';
import { STAGE_DEFINITIONS, STAGE_PREREQUISITES } from './stages.js';
function stageSlug(stage) {
    return stage.replace(/_/g, '-');
}
function toRelatedLinkType(stage) {
    switch (stage) {
        case 'research':
            return 'research';
        case 'consensus':
            return 'consensus';
        case 'architecture_decision':
            return 'adr';
        case 'specification':
            return 'spec';
        default:
            return 'file';
    }
}
function buildDefaultBody(epicId, stage) {
    const stageLabel = STAGE_DEFINITIONS[stage].name;
    return [
        `# ${stageLabel} (${epicId})`,
        '',
        '## Summary',
        '',
        `Lifecycle artifact for stage \`${stage}\` on epic \`${epicId}\`.`,
        '',
        '## Notes',
        '',
    ].join('\n');
}
function toWorkspaceRelative(path, cwd) {
    const projectRoot = getProjectRoot(cwd);
    return relative(projectRoot, path).replaceAll('\\', '/');
}
function buildRelatedLinks(epicId, stage, absolutePath, cwd) {
    const prereqs = STAGE_PREREQUISITES[stage] ?? [];
    const related = [{ type: 'task', id: epicId }];
    const artifactDir = dirname(absolutePath);
    for (const prereq of prereqs) {
        const prereqDir = getStagePath(epicId, prereq, cwd);
        const prereqFile = join(prereqDir, `${epicId}-${stageSlug(prereq)}.md`);
        if (!existsSync(prereqFile)) {
            continue;
        }
        related.push({
            type: toRelatedLinkType(prereq),
            path: relative(artifactDir, prereqFile).replaceAll('\\', '/'),
        });
    }
    return related;
}
/**
 * Ensure stage artifact exists and frontmatter/backlinks are up to date.
 */
export async function ensureStageArtifact(epicId, stage, cwd) {
    const stageDir = ensureStagePath(epicId, stage, cwd);
    const fileName = `${epicId}-${stageSlug(stage)}.md`;
    const absolutePath = join(stageDir, fileName);
    const outputFile = toWorkspaceRelative(absolutePath, cwd);
    const currentContent = existsSync(absolutePath)
        ? await readFile(absolutePath, 'utf-8')
        : buildDefaultBody(epicId, stage);
    const related = buildRelatedLinks(epicId, stage, absolutePath, cwd);
    const metadata = buildFrontmatter(epicId, stage, {
        task: epicId,
        related,
    });
    const nextContent = addFrontmatter(currentContent, metadata);
    await writeFile(absolutePath, nextContent, 'utf-8');
    return {
        absolutePath,
        outputFile,
        related,
    };
}
//# sourceMappingURL=stage-artifacts.js.map