import pytest
from unittest.mock import AsyncMock, patch, MagicMock
from app.yandex_gpt import YandexGPT


@pytest.fixture
def gpt():
    return YandexGPT()


@pytest.mark.asyncio
async def test_analyze_intent_question(gpt):
    gpt._load_credentials = AsyncMock(return_value=True)

    with patch("app.yandex_gpt.YandexGPT._request", new=AsyncMock(return_value="question")):
        result = await gpt.analyze_intent("Что вы делаете?")
        assert result == "question"


@pytest.mark.asyncio
async def test_analyze_intent_ticket(gpt):
    gpt._load_credentials = AsyncMock(return_value=True)

    with patch("app.yandex_gpt.YandexGPT._request", new=AsyncMock(return_value="ticket")):
        result = await gpt.analyze_intent("Хочу заказать аудит")
        assert result == "ticket"


@pytest.mark.asyncio
async def test_analyze_intent_unknown(gpt):
    gpt._load_credentials = AsyncMock(return_value=True)

    with patch("app.yandex_gpt.YandexGPT._request", new=AsyncMock(return_value=None)):
        result = await gpt.analyze_intent("абырвалг")
        assert result == "unknown"


@pytest.mark.asyncio
async def test_analyze_intent_unavailable(gpt):
    gpt._load_credentials = AsyncMock(return_value=False)
    result = await gpt.analyze_intent("Что вы делаете?")
    assert result == "unknown"


@pytest.mark.asyncio
async def test_generate_answer(gpt):
    gpt._load_credentials = AsyncMock(return_value=True)

    with patch("app.yandex_gpt.YandexGPT._request", new=AsyncMock(return_value="Мы предоставляем услуги аудита...")):
        result = await gpt.generate_answer("Какие услуги?", "контекст")
        assert result is not None
        assert "аудита" in result


@pytest.mark.asyncio
async def test_generate_answer_unavailable(gpt):
    gpt._load_credentials = AsyncMock(return_value=False)
    result = await gpt.generate_answer("Какие услуги?", "контекст")
    assert result is None
