import { runCheckpoint } from '../engine/session-safeguard/checkpoint-runner.js';
import { readPlanuConfig } from '../engine/planu-config-writer.js';
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
const DEFAULT_AUTOPUSH_BRANCHES = ['feat/*', 'fix/*', 'chore/*'];
function resolveProjectPath(input) {
    return input ?? process.cwd();
}
async function loadProjectConfig(projectPath) {
    const configPath = join(projectPath, 'planu', 'config.json');
    const config = await readPlanuConfig(configPath, readFile);
    return {
        autopush: config?.autopush ?? false,
        autopushBranches: config?.autopushBranches ?? DEFAULT_AUTOPUSH_BRANCHES,
    };
}
export async function handlePlanSessionCheckpoint(input) {
    const projectPath = resolveProjectPath(input.projectPath);
    const { autopush, autopushBranches } = await loadProjectConfig(projectPath);
    const result = await runCheckpoint({
        projectPath,
        dryRun: input.dryRun ?? false,
        autopush,
        autopushBranches,
    });
    const lines = [result.humanSummary, ''];
    if (result.unpushedCommits && result.unpushedCommits.oldestAgeHours >= 2) {
        const u = result.unpushedCommits;
        lines.push(`WARNING: ${String(u.count)} commit(s) unpushed for ${String(u.oldestAgeHours)}h on branch ${u.branch}. Run git push or enable autopush.`);
    }
    if (result.pushed?.pushed === true) {
        lines.push(`Pushed ${String(result.pushed.commitsPushed)} commit(s) to ${result.pushed.ref}`);
    }
    lines.push('');
    lines.push(`session-context refreshed: ${String(result.sessionContextRefreshed)}`);
    lines.push(`learnings flushed: ${String(result.learningsFlushed)}`);
    lines.push(`unpushed commits: ${String(result.unpushedCommits?.count ?? 0)}`);
    return {
        content: [{ type: 'text', text: lines.join('\n').trim() }],
    };
}
//# sourceMappingURL=planu-session-checkpoint.js.map