package com.cameleer.server.core.alerting;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Map;

public record ExchangeMatchCondition(
        AlertScope scope,
        ExchangeFilter filter,
        FireMode fireMode,
        Integer threshold,                // required when COUNT_IN_WINDOW; null for PER_EXCHANGE
        Integer windowSeconds             // required when COUNT_IN_WINDOW
) implements AlertCondition {

    public ExchangeMatchCondition {
        if (fireMode == null)
            throw new IllegalArgumentException("fireMode is required (PER_EXCHANGE or COUNT_IN_WINDOW)");
        if (fireMode == FireMode.COUNT_IN_WINDOW && (threshold == null || windowSeconds == null))
            throw new IllegalArgumentException("COUNT_IN_WINDOW requires threshold + windowSeconds");
    }

    @Override
    @JsonProperty(value = "kind", access = JsonProperty.Access.READ_ONLY)
    public ConditionKind kind() { return ConditionKind.EXCHANGE_MATCH; }

    public record ExchangeFilter(String status, Map<String, String> attributes) {
        public ExchangeFilter { attributes = attributes == null ? Map.of() : Map.copyOf(attributes); }
    }
}
