using StackExchange.Redis;

public class AlertSuppressionService : IAlertSuppressionService
{
    private readonly IConnectionMultiplexer _redis;
    private readonly ClinicalMetrics _metrics;

    public AlertSuppressionService(IConnectionMultiplexer redis, ClinicalMetrics metrics)
    {
        _redis = redis;
        _metrics = metrics;
    }

    public static string SuppressionKey(Guid encounterId, AlertType alertType) =>
        $"suppress:{encounterId}:{alertType.ToDbString()}";

    public async Task SetSuppressionAsync(
        Guid encounterId, AlertType alertType, TimeSpan ttl, CancellationToken ct = default)
    {
        var cache = _redis.GetDatabase();
        await cache.StringSetAsync(SuppressionKey(encounterId, alertType), "1", ttl);
        _metrics.AlertSuppressionsTotal.WithLabels(alertType.ToDbString()).Inc();
    }

    public async Task<bool> IsSuppressedAsync(
        Guid encounterId, AlertType alertType, CancellationToken ct = default)
    {
        var cache = _redis.GetDatabase();
        return await cache.KeyExistsAsync(SuppressionKey(encounterId, alertType));
    }
}