package com.cameleer.server.core.alerting;

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

public interface AlertRuleRepository {
    AlertRule save(AlertRule rule);                       // upsert by id
    Optional<AlertRule> findById(UUID id);
    List<AlertRule> listByEnvironment(UUID environmentId);
    List<AlertRule> findAllByOutboundConnectionId(UUID connectionId);
    List<UUID> findRuleIdsByOutboundConnectionId(UUID connectionId);  // used by rulesReferencing()
    void delete(UUID id);

    /** Claim up to batchSize rules whose next_evaluation_at <= now AND (claimed_until IS NULL OR claimed_until < now).
     *  Atomically sets claimed_by + claimed_until = now + ttl. Returns claimed rules. */
    List<AlertRule> claimDueRules(String instanceId, int batchSize, int claimTtlSeconds);

    /** Release claim + bump next_evaluation_at. */
    void releaseClaim(UUID ruleId, Instant nextEvaluationAt, Map<String, Object> evalState);
}
