import fs from 'fs';
import path from 'path';
import { execFileSync } from 'child_process';
import { SynapseConfig } from '../config.js';
import { resolveSynapsePaths } from '../db/client.js';
const pending = [];
let commitTimer = null;
function isGitRepo(root) {
    return fs.existsSync(path.join(root, '.git'));
}
export function renderFieldReportMarkdown(report) {
    const metadata = JSON.stringify(report);
    return [
        `<!-- synapse-field-report:${metadata} -->`,
        `# Field Report: ${report.missionTitle}`,
        '',
        `- Operative: ${report.operativeId}`,
        `- Sortie: ${report.sortieId}`,
        `- Status: ${report.status}`,
        `- Tokens: ${report.tokensUsed}`,
        `- Cost: ${report.costUsd}`,
        '',
        '## Findings',
        report.findings || 'None recorded.',
        '',
        '## Files Changed',
        ...(report.filesChanged.length ? report.filesChanged.map((entry) => `- ${entry}`) : ['- none']),
        '',
        '## Blockers',
        report.blockersEncountered || 'None',
        '',
        '## Next Recommended Action',
        report.nextRecommendedAction || 'None',
        '',
    ].join('\n');
}
export function writeLedgerMarkdown(root, report) {
    try {
        const ledgerRoot = resolveSynapsePaths(root).ledgerDir;
        const dir = path.join(ledgerRoot, report.strikeTeamId ?? 'solo', report.operativeId);
        fs.mkdirSync(dir, { recursive: true });
        const filePath = path.join(dir, `${report.id}.md`);
        fs.writeFileSync(filePath, renderFieldReportMarkdown(report), 'utf8');
        return filePath;
    }
    catch {
        return null;
    }
}
export function scheduleFieldReportLedgerExport(root, report) {
    const filePath = writeLedgerMarkdown(root, report);
    if (filePath)
        pending.push({ root, filePath });
    return filePath;
}
export function startLedgerBatchCommitter() {
    if (commitTimer || !SynapseConfig.ledgerEnabled)
        return;
    commitTimer = setInterval(() => {
        if (pending.length === 0)
            return;
        const batch = pending.splice(0, pending.length);
        const byRoot = new Map();
        batch.forEach((entry) => {
            const list = byRoot.get(entry.root) ?? [];
            list.push(entry.filePath);
            byRoot.set(entry.root, list);
        });
        for (const [root, files] of byRoot) {
            if (!isGitRepo(root))
                continue;
            try {
                execFileSync('git', ['add', ...files], { cwd: root, timeout: 5_000 });
                execFileSync('git', ['commit', '-m', `synapse: field reports [${files.length}]`], { cwd: root, timeout: 10_000 });
            }
            catch {
                files.forEach((filePath) => pending.push({ root, filePath }));
            }
        }
    }, SynapseConfig.ledgerCommitIntervalMs);
    commitTimer.unref();
}
export function stopLedgerBatchCommitter() {
    if (commitTimer)
        clearInterval(commitTimer);
    commitTimer = null;
}
