// engine/checkpoint/checkpoint-manager.ts — Checkpoint lifecycle management (SPEC-411)
import { hashProjectPath } from '../../storage/base-store.js';
import { createWorkflowCheckpoint, resolveWorkflowCheckpoint, getPendingWorkflowCheckpoints, } from '../../storage/workflow-checkpoint-store.js';
// ---------------------------------------------------------------------------
// Request
// ---------------------------------------------------------------------------
/**
 * Creates a new pending checkpoint for the given spec transition.
 * If the policy has autoApproveAfterHours set, schedules the auto-approve time.
 */
export async function requestCheckpoint(projectPath, specId, policy, requestedBy) {
    const projectId = hashProjectPath(projectPath);
    const autoApproveAt = policy.autoApproveAfterHours !== undefined
        ? new Date(Date.now() + policy.autoApproveAfterHours * 3_600_000).toISOString()
        : undefined;
    return createWorkflowCheckpoint(projectPath, {
        specId,
        projectId,
        policyId: policy.id,
        blockingTransition: policy.transition,
        requiredRole: policy.requiredRole,
        requestedBy,
        requestedAt: new Date().toISOString(),
        status: 'pending',
        autoApproveAt,
    });
}
// ---------------------------------------------------------------------------
// Approve
// ---------------------------------------------------------------------------
/**
 * Approves a pending checkpoint.
 * Returns the updated checkpoint and a flag indicating the blocked transition can now proceed.
 */
export async function approveCheckpoint(projectPath, checkpointId, approvedBy) {
    const checkpoint = await resolveWorkflowCheckpoint(projectPath, checkpointId, {
        status: 'approved',
        resolvedBy: approvedBy,
    });
    return { checkpoint, transitionApplied: true };
}
// ---------------------------------------------------------------------------
// Reject
// ---------------------------------------------------------------------------
/** Rejects a pending checkpoint with a mandatory reason. */
export async function rejectCheckpoint(projectPath, checkpointId, rejectedBy, reason) {
    return resolveWorkflowCheckpoint(projectPath, checkpointId, {
        status: 'rejected',
        resolvedBy: rejectedBy,
        rejectionReason: reason,
    });
}
// ---------------------------------------------------------------------------
// Auto-approve
// ---------------------------------------------------------------------------
/**
 * Scans all pending checkpoints for a project and auto-approves any that have
 * passed their autoApproveAt deadline. Returns the list of auto-approved checkpoints.
 */
export async function checkAutoApprovals(projectPath) {
    const pending = await getPendingWorkflowCheckpoints(projectPath);
    const now = Date.now();
    const autoApproved = [];
    for (const checkpoint of pending) {
        if (checkpoint.autoApproveAt !== undefined &&
            new Date(checkpoint.autoApproveAt).getTime() <= now) {
            const updated = await resolveWorkflowCheckpoint(projectPath, checkpoint.id, {
                status: 'approved',
                resolvedBy: 'system:auto-approve',
            });
            autoApproved.push(updated);
        }
    }
    return autoApproved;
}
//# sourceMappingURL=checkpoint-manager.js.map