"""Alertmanager LLM storm guards.

Service-layer Redis helpers used by webhook routers to avoid spawning duplicate
LLM analysis tasks for the same Alertmanager fingerprint.
"""

from src.core.logging import get_logger
from src.core.redis_client import get_redis

logger = get_logger("awoooi.alertmanager_llm_guard")

ALERTMANAGER_LLM_INFLIGHT_LOCK_TTL_SECONDS = 600


def alertmanager_llm_inflight_key(fingerprint: str) -> str:
    """Return the Redis lock key for one Alertmanager fingerprint entering AI analysis."""

    return f"alertmanager:llm_inflight:{fingerprint}"


async def try_acquire_alertmanager_llm_lock(
    fingerprint: str,
    alert_id: str,
    *,
    ttl_seconds: int = ALERTMANAGER_LLM_INFLIGHT_LOCK_TTL_SECONDS,
) -> bool:
    """Prevent same-second duplicate Alertmanager deliveries from spawning LLM calls."""

    try:
        redis = get_redis()
        acquired = await redis.set(
            alertmanager_llm_inflight_key(fingerprint),
            alert_id,
            ex=ttl_seconds,
            nx=True,
        )
        return bool(acquired)
    except Exception as exc:
        logger.warning(
            "alertmanager_llm_inflight_lock_failed_fail_open",
            fingerprint=fingerprint,
            alert_id=alert_id,
            error=str(exc),
        )
        return True
