package com.cameleer.server.app.alerting.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("cameleer.server.alerting")
public record AlertingProperties(
        Integer evaluatorTickIntervalMs,
        Integer evaluatorBatchSize,
        Integer claimTtlSeconds,
        Integer notificationTickIntervalMs,
        Integer notificationBatchSize,
        Boolean inTickCacheEnabled,
        Integer circuitBreakerFailThreshold,
        Integer circuitBreakerWindowSeconds,
        Integer circuitBreakerCooldownSeconds,
        Integer eventRetentionDays,
        Integer notificationRetentionDays,
        Integer webhookTimeoutMs,
        Integer webhookMaxAttempts,
        Integer perExchangeDeployBacklogCapSeconds) {

    public int effectiveEvaluatorTickIntervalMs() {
        int raw = evaluatorTickIntervalMs == null ? 5000 : evaluatorTickIntervalMs;
        return Math.max(5000, raw);  // floor: no faster than 5 s
    }

    public int effectiveEvaluatorBatchSize() {
        return evaluatorBatchSize == null ? 20 : evaluatorBatchSize;
    }

    public int effectiveClaimTtlSeconds() {
        return claimTtlSeconds == null ? 30 : claimTtlSeconds;
    }

    public int effectiveNotificationTickIntervalMs() {
        return notificationTickIntervalMs == null ? 5000 : notificationTickIntervalMs;
    }

    public int effectiveNotificationBatchSize() {
        return notificationBatchSize == null ? 50 : notificationBatchSize;
    }

    public boolean effectiveInTickCacheEnabled() {
        return inTickCacheEnabled == null || inTickCacheEnabled;
    }

    public int effectiveEventRetentionDays() {
        return eventRetentionDays == null ? 90 : eventRetentionDays;
    }

    public int effectiveNotificationRetentionDays() {
        return notificationRetentionDays == null ? 30 : notificationRetentionDays;
    }

    public int effectiveWebhookTimeoutMs() {
        return webhookTimeoutMs == null ? 5000 : webhookTimeoutMs;
    }

    public int effectiveWebhookMaxAttempts() {
        return webhookMaxAttempts == null ? 3 : webhookMaxAttempts;
    }

    public int cbFailThreshold() {
        return circuitBreakerFailThreshold == null ? 5 : circuitBreakerFailThreshold;
    }

    public int cbWindowSeconds() {
        return circuitBreakerWindowSeconds == null ? 30 : circuitBreakerWindowSeconds;
    }

    public int cbCooldownSeconds() {
        return circuitBreakerCooldownSeconds == null ? 60 : circuitBreakerCooldownSeconds;
    }

    public int effectivePerExchangeDeployBacklogCapSeconds() {
        // Default 24 h. Zero or negative = disabled (no clamp — first-run uses rule.createdAt as today).
        return perExchangeDeployBacklogCapSeconds == null ? 86_400 : perExchangeDeployBacklogCapSeconds;
    }
}
