from __future__ import annotations

from uuid import uuid4

import pytest

from src.api.v1.platform import events
from src.core.awooop_operator_auth import AwoooPOperatorPrincipal


@pytest.mark.asyncio
async def test_create_source_provider_heartbeat_records_low_noise_events(monkeypatch):
    captured: list[dict] = []

    async def fake_record_external_alert_event(**kwargs):
        captured.append(kwargs)
        return uuid4()

    monkeypatch.setattr(events, "record_external_alert_event", fake_record_external_alert_event)

    response = await events.create_source_provider_heartbeat(
        events.SourceProviderHeartbeatRequest(
            providers=["sentry", "signoz"],
            reason="test_provider_freshness",
            run_ref="run-123",
        ),
        AwoooPOperatorPrincipal(
            operator_id="gitea-e2e-health",
            auth_method="operator_api_key",
        ),
    )

    assert response["status"] == "recorded"
    assert [item["provider"] for item in response["items"]] == ["sentry", "signoz"]
    assert [item["event_id"] for item in response["items"]] == [
        captured[0]["event_id"],
        captured[1]["event_id"],
    ]

    for call in captured:
        assert call["stage"] == "heartbeat"
        assert call["severity"] == "info"
        assert call["title"] == "SourceProviderHeartbeat"
        assert call["target_resource"] == "source-provider-ingestion"
        assert call["labels"]["telegram"] == "not_sent"
        assert call["labels"]["incident"] == "not_created"
        assert call["payload"]["side_effects"] == {
            "incident_created": False,
            "telegram_sent": False,
            "approval_created": False,
        }
