from __future__ import annotations

from pathlib import Path

import yaml


ROOT = Path(__file__).resolve().parents[3]
ALERTMANAGER_CONFIG = ROOT / "ops" / "alertmanager" / "alertmanager.yml"


def _config() -> dict:
    return yaml.safe_load(ALERTMANAGER_CONFIG.read_text(encoding="utf-8"))


def _receiver(name: str) -> dict:
    return next(item for item in _config()["receivers"] if item["name"] == name)


def test_awoooi_webhook_uses_public_api_route_not_dead_vip() -> None:
    receiver = _receiver("awoooi-webhook")
    urls = [
        item["url"]
        for item in receiver.get("webhook_configs", [])
        if item.get("url")
    ]

    assert urls == ["https://awoooi.wooo.work/api/v1/webhooks/alertmanager"]
    assert all("192.168.0.125:32334" not in url for url in urls)
    assert all("/api/v1/webhooks/alertmanager" in url for url in urls)


def test_emergency_direct_telegram_stays_limited_to_alert_chain() -> None:
    receiver = _receiver("telegram-direct")
    assert receiver.get("telegram_configs")

    route_text = str(_config()["route"])
    assert "AWOOOIApiDown" in route_text
    assert 'severity="critical"' in route_text
