using System;
using Svrnty.CQRS.Events.Abstractions.Delivery;
using Svrnty.CQRS.Events.Abstractions.EventStore;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Svrnty.CQRS.Events.Abstractions.Delivery;
///
/// Extended delivery provider interface for cross-service event delivery via external message brokers.
///
///
///
/// This interface extends with capabilities for publishing
/// events to external services and subscribing to events from external services.
///
///
/// Use Cases:
/// - Publishing events to RabbitMQ for consumption by other microservices
/// - Publishing events to Kafka for high-throughput scenarios
/// - Publishing events to Azure Service Bus or AWS SNS/SQS
/// - Subscribing to events from other services via message brokers
///
///
/// Phase 4 Implementation:
/// RabbitMQ provider with automatic topology management.
///
///
public interface IExternalEventDeliveryProvider : IEventDeliveryProvider
{
///
/// Publish an event to an external service via the message broker.
///
/// The name of the stream (maps to exchange/topic).
/// The event to publish.
/// Additional metadata (routing keys, headers, etc.).
/// Cancellation token.
/// A task that completes when the event is published.
///
///
/// This method is called by the stream store when an event needs to be published
/// externally (when StreamScope = CrossService).
///
///
/// The provider is responsible for:
/// - Serializing the event to the wire format
/// - Publishing to the appropriate exchange/topic
/// - Adding routing metadata (correlation ID, event type, etc.)
/// - Handling publish failures (retry, dead letter, etc.)
///
///
Task PublishExternalAsync(
string streamName,
ICorrelatedEvent @event,
IDictionary? metadata = null,
CancellationToken cancellationToken = default);
///
/// Subscribe to events from an external service via the message broker.
///
/// The name of the remote stream (maps to exchange/topic).
/// The subscription identifier (maps to queue name).
/// The consumer identifier (for consumer groups).
/// Handler called when events are received.
/// Cancellation token.
/// A task that represents the subscription lifecycle.
///
///
/// This method establishes a subscription to an external event stream from another service.
/// The provider is responsible for:
/// - Creating the necessary topology (queue, bindings, etc.)
/// - Deserializing incoming messages
/// - Invoking the event handler
/// - Managing acknowledgments and redelivery
///
///
/// The subscription remains active until the cancellation token is triggered or
/// an unrecoverable error occurs.
///
///
Task SubscribeExternalAsync(
string streamName,
string subscriptionId,
string consumerId,
Func, CancellationToken, Task> eventHandler,
CancellationToken cancellationToken = default);
///
/// Unsubscribe from an external event stream.
///
/// The name of the remote stream.
/// The subscription identifier.
/// The consumer identifier.
/// Cancellation token.
/// A task that completes when the unsubscription is finished.
///
/// This method cleans up resources associated with the subscription.
/// Depending on the provider, this may delete queues or simply disconnect.
///
Task UnsubscribeExternalAsync(
string streamName,
string subscriptionId,
string consumerId,
CancellationToken cancellationToken = default);
///
/// Check if this provider supports the specified stream for external delivery.
///
/// The stream name to check.
/// True if the provider can handle this stream, false otherwise.
///
/// This allows routing different streams to different providers based on configuration.
/// For example, "orders.*" might route to RabbitMQ while "analytics.*" routes to Kafka.
///
bool SupportsStream(string streamName);
}