import { getFrontmatterBlock, getFrontmatterField, } from '../../shared/frontmatter.js';
import { replaceFrontmatter, upsertFrontmatterField, } from '../../shared/frontmatter-write.js';
function replaceLine(content, pattern, nextLine) {
    return pattern.test(content) ? content.replace(pattern, nextLine) : content;
}
function findSectionBounds(content, heading) {
    const marker = `## ${heading}\n\n`;
    const start = content.indexOf(marker);
    if (start === -1) {
        return null;
    }
    const bodyStart = start + marker.length;
    const nextHeading = content.indexOf('\n## ', bodyStart);
    return {
        start,
        bodyStart,
        end: nextHeading === -1 ? content.length : nextHeading,
    };
}
function readSectionBody(content, heading) {
    const bounds = findSectionBounds(content, heading);
    if (!bounds) {
        return '';
    }
    return content.slice(bounds.bodyStart, bounds.end).trim();
}
function replaceSection(content, heading, body) {
    const bounds = findSectionBounds(content, heading);
    if (!bounds) {
        return content;
    }
    return [
        content.slice(0, bounds.start),
        `## ${heading}\n\n${body.trim()}\n`,
        content.slice(bounds.end),
    ].join('');
}
function renderCompletedProgress(content) {
    const existingLines = readSectionBody(content, 'Progress')
        .split('\n')
        .map((line) => line.trim())
        .filter((line) => line.length > 0 && line.startsWith('- ✓'));
    if (!existingLines.includes('- ✓ Project lifecycle complete')) {
        existingLines.push('- ✓ Project lifecycle complete');
    }
    return existingLines.join('\n');
}
export function renderCompletedProjectState(content, options) {
    const frontmatter = getFrontmatterBlock(content);
    if (!frontmatter) {
        throw new Error('state.md is missing frontmatter');
    }
    let nextBlock = upsertFrontmatterField(frontmatter, 'oat_lifecycle', 'complete', true).nextBlock;
    nextBlock = upsertFrontmatterField(nextBlock, 'oat_project_completed', `"${options.nowUtc}"`, true).nextBlock;
    nextBlock = upsertFrontmatterField(nextBlock, 'oat_project_state_updated', `"${options.nowUtc}"`, true).nextBlock;
    let nextContent = nextBlock === frontmatter
        ? content
        : replaceFrontmatter(content, nextBlock);
    nextContent = replaceLine(nextContent, /^\*\*Status:\*\*.*$/m, '**Status:** Complete');
    nextContent = replaceLine(nextContent, /^\*\*Last Updated:\*\*.*$/m, `**Last Updated:** ${options.today}`);
    const currentPhase = options.archived
        ? 'Lifecycle complete; archived locally'
        : 'Lifecycle complete';
    nextContent = replaceSection(nextContent, 'Current Phase', currentPhase);
    nextContent = replaceSection(nextContent, 'Progress', renderCompletedProgress(nextContent));
    nextContent = replaceSection(nextContent, 'Next Milestone', 'None. Project complete.');
    const currentLifecycle = getFrontmatterField(nextBlock, 'oat_lifecycle');
    if (currentLifecycle !== 'complete') {
        throw new Error('Failed to set oat_lifecycle: complete');
    }
    return nextContent;
}
