import pytest
from unittest.mock import AsyncMock, MagicMock, patch
from app.max_api import MaxAPI


@pytest.fixture
def api():
    return MaxAPI()


@pytest.mark.asyncio
async def test_subscribe_webhook(api):
    mock_response = MagicMock()
    mock_response.json.return_value = {"success": True}

    with patch.object(api.client, "post", new=AsyncMock(return_value=mock_response)):
        result = await api.subscribe_webhook()
        assert result == {"success": True}


@pytest.mark.asyncio
async def test_send_message(api):
    mock_response = MagicMock()
    mock_response.json.return_value = {"ok": True}

    with patch.object(api.client, "post", new=AsyncMock(return_value=mock_response)):
        result = await api.send_message(12345, "test", format="markdown")
        assert result == {"ok": True}


@pytest.mark.asyncio
async def test_send_message_with_keyboard(api):
    mock_response = MagicMock()
    mock_response.json.return_value = {"ok": True}
    keyboard = [{"type": "inline_keyboard", "payload": {"buttons": [[{"type": "callback", "text": "OK", "payload": "test"}]]}}]

    with patch.object(api.client, "post", new=AsyncMock(return_value=mock_response)):
        result = await api.send_message(12345, "test", attachments=keyboard)
        assert result == {"ok": True}


@pytest.mark.asyncio
async def test_answer_callback_with_notification(api):
    mock_response = MagicMock()
    mock_response.json.return_value = {"success": True}

    with patch.object(api.client, "post", new=AsyncMock(return_value=mock_response)):
        result = await api.answer_callback("cb_123", notification="OK")
        assert result == {"success": True}


@pytest.mark.asyncio
async def test_headers(api):
    headers = api._headers()
    assert "Authorization" in headers
    assert "Content-Type" in headers
    assert headers["Content-Type"] == "application/json"
