using System.Threading;
using Svrnty.CQRS.Events.Abstractions.EventStore;
using System.Threading.Tasks;
namespace Svrnty.CQRS.Events.Abstractions.Projections;
///
/// Represents a projection that processes events to build a read model.
///
/// The type of event this projection handles.
///
///
/// Phase 7 Feature - Event Sourcing Projections:
/// Projections consume events from streams and build queryable read models.
/// Each projection maintains a checkpoint to track its position in the stream.
///
///
/// Key Concepts:
/// - Projections are idempotent (can process same event multiple times safely)
/// - Projections can be rebuilt by replaying events from the beginning
/// - Multiple projections can consume the same stream independently
/// - Projections run asynchronously and eventually consistent with the stream
///
///
public interface IProjection where TEvent : ICorrelatedEvent
{
///
/// Handles an event and updates the read model accordingly.
///
/// The event to process.
/// Cancellation token.
/// A task representing the async operation.
///
///
/// This method should be idempotent - processing the same event multiple times
/// should produce the same result. This is critical for projection rebuilding.
///
///
/// If this method throws an exception, the projection engine will retry based on
/// its configured retry policy. Persistent failures may require manual intervention.
///
///
Task HandleAsync(TEvent @event, CancellationToken cancellationToken = default);
}
///
/// Represents a projection that can handle any event type dynamically.
///
///
/// Use this interface when you need to handle multiple event types in a single projection
/// or when event types are not known at compile time.
///
public interface IDynamicProjection
{
///
/// Handles an event dynamically and updates the read model accordingly.
///
/// The event to process.
/// Cancellation token.
/// A task representing the async operation.
Task HandleAsync(ICorrelatedEvent @event, CancellationToken cancellationToken = default);
}
///
/// Marker interface for projections that support rebuilding.
///
///
///
/// Projections implementing this interface can be rebuilt from scratch by:
/// 1. Calling ResetAsync() to clear the read model
/// 2. Replaying all events from the beginning
/// 3. Processing events through HandleAsync()
///
///
/// This is useful for:
/// - Fixing bugs in projection logic
/// - Schema migrations in the read model
/// - Adding new projections to existing streams
///
///
public interface IResettableProjection
{
///
/// Resets the projection's read model to its initial state.
///
/// Cancellation token.
/// A task representing the async operation.
///
/// This method should delete or clear all data in the read model.
/// After calling this, the projection can be rebuilt from offset 0.
///
Task ResetAsync(CancellationToken cancellationToken = default);
}