// storage/workflow-checkpoint-store.ts — Workflow checkpoint persistence (SPEC-411)
// Layout: data/projects/{hash}/workflow-checkpoints.json
import { randomUUID } from 'node:crypto';
import { readJson, writeJson, projectDataDir, hashProjectPath } from './base-store.js';
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function storeFilePath(projectId) {
    return `${projectDataDir(projectId)}/workflow-checkpoints.json`;
}
async function loadAll(projectId) {
    return readJson(storeFilePath(projectId), []);
}
async function saveAll(projectId, checkpoints) {
    await writeJson(storeFilePath(projectId), checkpoints);
}
// ---------------------------------------------------------------------------
// CRUD operations
// ---------------------------------------------------------------------------
/** Persist a new checkpoint and return it with its generated ID. */
export async function createWorkflowCheckpoint(projectPath, checkpoint) {
    const projectId = hashProjectPath(projectPath);
    const all = await loadAll(projectId);
    const record = { id: randomUUID(), ...checkpoint };
    all.push(record);
    await saveAll(projectId, all);
    return record;
}
/** Resolve a checkpoint (approve or reject). Returns the updated checkpoint. */
export async function resolveWorkflowCheckpoint(projectPath, checkpointId, resolution) {
    const projectId = hashProjectPath(projectPath);
    const all = await loadAll(projectId);
    const idx = all.findIndex((c) => c.id === checkpointId);
    if (idx === -1) {
        throw new Error(`[Planu] Checkpoint not found: ${checkpointId}`);
    }
    const existing = all[idx];
    /* v8 ignore next 3 */
    if (!existing) {
        throw new Error(`[Planu] Checkpoint not found: ${checkpointId}`);
    }
    const updated = {
        ...existing,
        status: resolution.status,
        resolvedBy: resolution.resolvedBy,
        resolvedAt: new Date().toISOString(),
        rejectionReason: resolution.rejectionReason,
    };
    all[idx] = updated;
    await saveAll(projectId, all);
    return updated;
}
/** Return all pending checkpoints for a project, optionally filtered by role. */
export async function getPendingWorkflowCheckpoints(projectPath, role) {
    const projectId = hashProjectPath(projectPath);
    const all = await loadAll(projectId);
    return all.filter((c) => c.status === 'pending' && (role === undefined || c.requiredRole === role));
}
/** Return a single checkpoint by ID, or null if not found. */
export async function getWorkflowCheckpoint(projectPath, checkpointId) {
    const projectId = hashProjectPath(projectPath);
    const all = await loadAll(projectId);
    return all.find((c) => c.id === checkpointId) ?? null;
}
/** Return all checkpoints for a given spec. */
export async function getCheckpointsForSpec(projectPath, specId) {
    const projectId = hashProjectPath(projectPath);
    const all = await loadAll(projectId);
    return all.filter((c) => c.specId === specId);
}
//# sourceMappingURL=workflow-checkpoint-store.js.map