using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Svrnty.CQRS.Events.Abstractions.Sagas;
///
/// Persistent storage for saga state.
///
public interface ISagaStateStore
{
///
/// Save saga state.
///
/// The saga state to save.
/// Cancellation token.
Task SaveStateAsync(SagaStateSnapshot state, CancellationToken cancellationToken = default);
///
/// Load saga state.
///
/// The saga ID.
/// Cancellation token.
/// The saga state, or null if not found.
Task LoadStateAsync(string sagaId, CancellationToken cancellationToken = default);
///
/// Get all sagas for a correlation ID.
///
/// The correlation ID.
/// Cancellation token.
/// List of saga states.
Task> GetByCorrelationIdAsync(
string correlationId,
CancellationToken cancellationToken = default);
///
/// Get sagas by state.
///
/// The saga state to filter by.
/// Cancellation token.
/// List of saga states.
Task> GetByStateAsync(
SagaState state,
CancellationToken cancellationToken = default);
///
/// Delete saga state.
///
/// The saga ID to delete.
/// Cancellation token.
Task DeleteStateAsync(string sagaId, CancellationToken cancellationToken = default);
}
///
/// Snapshot of saga state for persistence.
///
public sealed record SagaStateSnapshot
{
///
/// Saga instance ID.
///
public required string SagaId { get; init; }
///
/// Correlation ID.
///
public required string CorrelationId { get; init; }
///
/// Saga type name.
///
public required string SagaName { get; init; }
///
/// Current state.
///
public SagaState State { get; init; }
///
/// Current step index.
///
public int CurrentStep { get; init; }
///
/// Total number of steps.
///
public int TotalSteps { get; init; }
///
/// Completed steps (for compensation tracking).
///
public List CompletedSteps { get; init; } = new();
///
/// When the saga started.
///
public DateTimeOffset StartedAt { get; init; }
///
/// When the saga was last updated.
///
public DateTimeOffset LastUpdated { get; init; }
///
/// When the saga completed (if completed).
///
public DateTimeOffset? CompletedAt { get; init; }
///
/// Error message (if failed).
///
public string? ErrorMessage { get; init; }
///
/// Saga data (serialized as JSON or similar).
///
public Dictionary Data { get; init; } = new();
}