using System.Threading;
using Svrnty.CQRS.Events.Abstractions.Models;
using System.Threading.Tasks;
namespace Svrnty.CQRS.Events.Abstractions.EventHandlers;
///
/// Handler interface for commands that participate in a workflow and return a result.
/// The workflow manages event emission and correlation.
///
/// The command type to handle.
/// The result type returned by the handler.
/// The workflow type that manages events for this command. Must inherit from .
///
///
/// Workflow Pattern:
/// Instead of manually managing event contexts and correlation IDs, handlers receive a workflow instance.
/// The workflow encapsulates the business process and provides methods to emit events.
/// All events emitted within the workflow are automatically correlated using the workflow's ID.
///
///
/// Example Usage:
///
/// public class InviteUserCommandHandler
/// : ICommandHandlerWithWorkflow<InviteUserCommand, string, InvitationWorkflow>
/// {
/// public async Task<string> HandleAsync(
/// InviteUserCommand command,
/// InvitationWorkflow workflow,
/// CancellationToken cancellationToken)
/// {
/// // Business logic
/// var invitationId = Guid.NewGuid().ToString();
///
/// // Emit event via workflow (automatically correlated)
/// workflow.Emit(new UserInvitedEvent
/// {
/// InvitationId = invitationId,
/// Email = command.Email
/// });
///
/// // Return workflow ID for follow-up commands
/// return workflow.Id;
/// }
/// }
///
///
///
/// Framework Behavior:
/// - The framework creates/loads the workflow instance before calling the handler
/// - Workflow.Id is set (either new GUID or existing workflow ID)
/// - Workflow.IsNew indicates if this is a new workflow or continuation
/// - After the handler completes, the framework emits all events collected in the workflow
/// - All events receive the workflow ID as their correlation ID
///
///
public interface ICommandHandlerWithWorkflow
where TCommand : class
where TWorkflow : Workflow
{
///
/// Handles the command within the context of a workflow.
///
/// The command to handle.
/// The workflow instance managing events for this command execution.
/// Cancellation token for the async operation.
/// The result of handling the command.
///
/// Emit events by calling methods on the workflow instance (which internally call workflow.Emit()).
/// The framework will persist all emitted events after this method completes successfully.
///
Task HandleAsync(
TCommand command,
TWorkflow workflow,
CancellationToken cancellationToken = default);
}
///
/// Handler interface for commands that participate in a workflow but do not return a result.
/// The workflow manages event emission and correlation.
///
/// The command type to handle.
/// The workflow type that manages events for this command. Must inherit from .
///
///
/// This is the "no result" variant of .
/// Use this when your command performs an action but doesn't need to return a value.
///
///
/// Example Usage:
///
/// public class DeclineInviteCommandHandler
/// : ICommandHandlerWithWorkflow<DeclineInviteCommand, InvitationWorkflow>
/// {
/// public async Task HandleAsync(
/// DeclineInviteCommand command,
/// InvitationWorkflow workflow,
/// CancellationToken cancellationToken)
/// {
/// workflow.Emit(new UserInviteDeclinedEvent
/// {
/// InvitationId = command.InvitationId,
/// Reason = command.Reason
/// });
///
/// await Task.CompletedTask;
/// }
/// }
///
///
///
public interface ICommandHandlerWithWorkflow
where TCommand : class
where TWorkflow : Workflow
{
///
/// Handles the command within the context of a workflow.
///
/// The command to handle.
/// The workflow instance managing events for this command execution.
/// Cancellation token for the async operation.
/// A task representing the async operation.
///
/// Emit events by calling methods on the workflow instance (which internally call workflow.Emit()).
/// The framework will persist all emitted events after this method completes successfully.
///
Task HandleAsync(
TCommand command,
TWorkflow workflow,
CancellationToken cancellationToken = default);
}