package com.cameleer.server.core.alerting;

import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public record AlertRule(
        UUID id,
        UUID environmentId,
        String name,
        String description,
        AlertSeverity severity,
        boolean enabled,
        ConditionKind conditionKind,
        AlertCondition condition,
        int evaluationIntervalSeconds,
        int forDurationSeconds,
        int reNotifyMinutes,
        String notificationTitleTmpl,
        String notificationMessageTmpl,
        List<WebhookBinding> webhooks,
        List<AlertRuleTarget> targets,
        Instant nextEvaluationAt,
        String claimedBy,
        Instant claimedUntil,
        Map<String, Object> evalState,
        Instant createdAt,
        String createdBy,
        Instant updatedAt,
        String updatedBy) {

    public AlertRule {
        webhooks  = webhooks  == null ? List.of() : List.copyOf(webhooks);
        targets   = targets   == null ? List.of() : List.copyOf(targets);
        evalState = evalState == null ? Map.of()  : Map.copyOf(evalState);
    }

    public AlertRule withEvalState(Map<String, Object> newEvalState) {
        return new AlertRule(
                id, environmentId, name, description, severity, enabled,
                conditionKind, condition, evaluationIntervalSeconds,
                forDurationSeconds, reNotifyMinutes,
                notificationTitleTmpl, notificationMessageTmpl,
                webhooks, targets, nextEvaluationAt, claimedBy, claimedUntil,
                newEvalState,
                createdAt, createdBy, updatedAt, updatedBy);
    }
}
