import { detectUnpushedCommits } from './unpushed-detector.js';
import { flushBuffer } from './learnings-buffer.js';
import { checkSessionContextFreshness } from './session-context-freshness.js';
import { safeAutopush } from './autopush.js';
const WARN_UNPUSHED_HOURS = 2;
/** Regenerate session-context.md (fire-and-forget, best-effort). */
async function regenerateSessionContext(projectPath) {
    try {
        const { generateSessionContext } = await import('../session-context-generator.js');
        const { resolveProjectIdOrAutoDetect } = await import('../../tools/resolve-project-id.js');
        const resolved = await resolveProjectIdOrAutoDetect({ projectPath });
        if (!resolved.ok) {
            return false;
        }
        await generateSessionContext(projectPath, resolved.projectId);
        return true;
    }
    catch {
        return false;
    }
}
/** Build the human-readable summary line for checkpoint output. */
function buildHumanSummary(sessionContextRefreshed, learningsFlushed, unpushed, pushed) {
    const parts = [];
    if (sessionContextRefreshed) {
        parts.push('session-context.md refreshed');
    }
    if (learningsFlushed > 0) {
        parts.push(`${String(learningsFlushed)} learning(s) flushed to MEMORY.md`);
    }
    if (pushed !== undefined) {
        if (pushed.pushed) {
            parts.push(`autopush: ${String(pushed.commitsPushed)} commit(s) pushed to ${pushed.ref}`);
        }
        else {
            parts.push(`autopush skipped (${pushed.reason})`);
        }
    }
    else if (unpushed && unpushed.oldestAgeHours >= WARN_UNPUSHED_HOURS) {
        parts.push(`WARNING: ${String(unpushed.count)} commit(s) unpushed for ${String(unpushed.oldestAgeHours)}h on branch ${unpushed.branch}. Run git push or enable autopush.`);
    }
    return parts.length > 0 ? parts.join('; ') : 'Checkpoint complete — nothing to do.';
}
/** Run a full session checkpoint: regen context, flush buffer, detect/push unpushed. */
export async function runCheckpoint(options) {
    const { projectPath, dryRun = false, autopush = false, autopushBranches } = options;
    // Check freshness and regenerate if stale (or always refresh on explicit call)
    const freshness = await checkSessionContextFreshness(projectPath);
    let sessionContextRefreshed = false;
    if (freshness.stale && !dryRun) {
        sessionContextRefreshed = await regenerateSessionContext(projectPath);
    }
    // Flush learnings buffer
    let learningsFlushed = 0;
    if (!dryRun) {
        learningsFlushed = await flushBuffer(projectPath).catch(() => 0);
    }
    // Detect unpushed commits
    const unpushedCommits = await detectUnpushedCommits(projectPath);
    // Autopush if configured
    let pushed;
    if (autopush && !dryRun) {
        pushed = await safeAutopush(projectPath, autopushBranches);
    }
    const humanSummary = buildHumanSummary(sessionContextRefreshed, learningsFlushed, unpushedCommits, pushed);
    return {
        sessionContextRefreshed,
        learningsFlushed,
        unpushedCommits,
        ...(pushed !== undefined ? { pushed } : {}),
        humanSummary,
    };
}
//# sourceMappingURL=checkpoint-runner.js.map