from types import SimpleNamespace
from unittest.mock import AsyncMock
from uuid import UUID

import pytest

from src.models.approval import ApprovalStatus
from src.services.telegram_gateway import TelegramGateway


@pytest.mark.asyncio
async def test_polling_reject_syncs_incident_state(monkeypatch):
    approval_id = UUID("44444444-4444-4444-4444-444444444444")
    approval = SimpleNamespace(
        id=approval_id,
        status=ApprovalStatus.REJECTED,
        incident_id="INC-20260513-POLLREJ",
    )
    sync_calls: list[dict] = []

    class _FakeApprovalService:
        async def reject_approval(self, **_kwargs):
            return approval, "Approval rejected"

    class _FakeIncidentApprovalService:
        async def on_approval_status_change(self, **kwargs):
            sync_calls.append(kwargs)

    monkeypatch.setattr(
        "src.services.approval_db.get_approval_service",
        lambda: _FakeApprovalService(),
    )
    monkeypatch.setattr(
        "src.services.incident_approval_service.get_incident_approval_service",
        lambda: _FakeIncidentApprovalService(),
    )
    monkeypatch.setattr(
        TelegramGateway,
        "_notify_approval_result",
        AsyncMock(return_value=None),
    )

    await TelegramGateway()._execute_approval_action(
        action="reject",
        approval_id=str(approval_id),
        user_id=42,
        username="ops",
        message_id=99,
    )

    assert sync_calls == [{
        "approval_id": str(approval_id),
        "new_status": "rejected",
    }]
