using System;
using Svrnty.CQRS.Events.Abstractions.Delivery;
using Svrnty.CQRS.Events.Abstractions.Streaming;
using Svrnty.CQRS.Events.Abstractions.Configuration;
using Svrnty.CQRS.Events.Abstractions;
namespace Svrnty.CQRS.Events.Configuration;
///
/// Default implementation of .
/// Provides configuration for an event stream's storage, delivery, and retention behavior.
///
public class StreamConfiguration : IStreamConfiguration
{
///
/// Initializes a new instance of the class.
///
/// Name of the stream. Must not be null or whitespace.
/// Thrown if is null or whitespace.
public StreamConfiguration(string streamName)
{
if (string.IsNullOrWhiteSpace(streamName))
throw new ArgumentException("Stream name cannot be null or whitespace.", nameof(streamName));
StreamName = streamName;
// Set defaults
Type = StreamType.Ephemeral;
DeliverySemantics = DeliverySemantics.AtLeastOnce;
Scope = StreamScope.Internal;
Retention = null;
EnableReplay = false;
}
///
public string StreamName { get; }
///
public StreamType Type { get; set; }
///
public DeliverySemantics DeliverySemantics { get; set; }
///
public StreamScope Scope { get; set; }
///
public TimeSpan? Retention { get; set; }
///
public bool EnableReplay { get; set; }
///
/// Validates the configuration settings.
///
///
/// Thrown if configuration is invalid (e.g., ephemeral stream with replay enabled).
///
public void Validate()
{
// Ephemeral streams cannot have replay enabled
if (Type == StreamType.Ephemeral && EnableReplay)
{
throw new InvalidOperationException(
$"Stream '{StreamName}': Ephemeral streams cannot have replay enabled. " +
"Set Type = StreamType.Persistent to enable replay.");
}
// Ephemeral streams shouldn't have retention policies
if (Type == StreamType.Ephemeral && Retention.HasValue)
{
throw new InvalidOperationException(
$"Stream '{StreamName}': Ephemeral streams do not support retention policies. " +
"Retention only applies to persistent streams.");
}
// Retention must be positive if set
if (Retention.HasValue && Retention.Value <= TimeSpan.Zero)
{
throw new InvalidOperationException(
$"Stream '{StreamName}': Retention period must be positive. " +
$"Got: {Retention.Value}");
}
}
}