// storage/checkpoint-store.ts — Checkpoint persistence (SPEC-063)
import { readdir, mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import { readJson, writeJson, projectDataDir } from './base-store.js';
import { writeJsonSafe } from '../engine/safety/atomic-writer.js';
// ---------------------------------------------------------------------------
// Paths
// ---------------------------------------------------------------------------
function checkpointsDir(projectId) {
    return join(projectDataDir(projectId), 'checkpoints');
}
function configPath(projectId) {
    return join(projectDataDir(projectId), 'context-budget.json');
}
// ---------------------------------------------------------------------------
// Checkpoint CRUD
// ---------------------------------------------------------------------------
/** Save a checkpoint to disk. Returns the checkpoint filename (ID). */
export async function saveCheckpoint(projectId, checkpoint) {
    const dir = checkpointsDir(projectId);
    await mkdir(dir, { recursive: true });
    const id = checkpoint.timestamp.replace(/[:.]/g, '-');
    const filePath = join(dir, `${id}.json`);
    await writeJsonSafe(filePath, checkpoint);
    return id;
}
/** Load a specific checkpoint or the most recent one. */
export async function loadCheckpoint(projectId, checkpointId) {
    const dir = checkpointsDir(projectId);
    if (checkpointId) {
        return readJson(join(dir, `${checkpointId}.json`), null);
    }
    // Load most recent
    const files = await listCheckpointFiles(dir);
    if (files.length === 0) {
        return null;
    }
    const latest = files[files.length - 1];
    /* c8 ignore next 3 */
    if (!latest) {
        return null;
    }
    return readJson(join(dir, latest), null);
}
/** List available checkpoints with summaries. */
export async function listCheckpoints(projectId, limit = 10) {
    const dir = checkpointsDir(projectId);
    const files = await listCheckpointFiles(dir);
    const recent = files.slice(-limit);
    const summaries = [];
    for (const file of recent) {
        const cp = await readJson(join(dir, file), null);
        if (cp) {
            summaries.push({
                id: file.replace('.json', ''),
                timestamp: cp.timestamp,
                summary: cp.summary.slice(0, 120),
            });
        }
    }
    return summaries;
}
// ---------------------------------------------------------------------------
// Config persistence
// ---------------------------------------------------------------------------
/** Load the context budget config for a project. */
export async function loadConfig(projectId) {
    return readJson(configPath(projectId), null);
}
/** Save the context budget config for a project. */
export async function saveConfig(projectId, config) {
    await writeJson(configPath(projectId), config);
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
async function listCheckpointFiles(dir) {
    try {
        const entries = await readdir(dir);
        return entries.filter((f) => f.endsWith('.json')).sort();
    }
    catch {
        return [];
    }
}
//# sourceMappingURL=checkpoint-store.js.map