"""Fixtures for API module tests."""

import pytest
from unittest.mock import MagicMock, patch


@pytest.fixture
def mock_llm():
    """Create a mock LLM."""
    mock = MagicMock()
    mock.invoke.return_value = MagicMock(content="Mocked LLM response")
    return mock


@pytest.fixture
def mock_search_engine():
    """Create a mock search engine."""
    mock = MagicMock()
    mock.run.return_value = [
        {
            "title": "Result 1",
            "link": "https://example.com/1",
            "snippet": "Snippet 1",
        },
        {
            "title": "Result 2",
            "link": "https://example.com/2",
            "snippet": "Snippet 2",
        },
    ]
    return mock


@pytest.fixture
def mock_search_system():
    """Create a mock AdvancedSearchSystem."""
    mock = MagicMock()
    mock.analyze_topic.return_value = {
        "current_knowledge": "Summary of findings",
        "findings": [{"content": "Finding 1"}, {"content": "Finding 2"}],
        "iterations": 2,
        "questions": {"1": ["Q1", "Q2"]},
        "formatted_findings": "Formatted findings text",
        "all_links_of_system": [
            {"title": "Source 1", "link": "https://example.com/1"},
        ],
    }
    mock.max_iterations = 2
    mock.questions_per_iteration = 3
    mock.model = MagicMock()
    return mock


@pytest.fixture
def mock_report_generator():
    """Create a mock report generator."""
    mock = MagicMock()
    mock.generate_report.return_value = {
        "content": "# Report\n\nReport content",
        "metadata": {"generated": "2024-01-01T00:00:00Z"},
    }
    return mock


@pytest.fixture
def sample_settings_snapshot():
    """Create a sample settings snapshot."""
    return {
        "llm.provider": {"value": "ollama", "ui_element": "select"},
        "llm.model": {"value": "gemma3:12b", "ui_element": "text"},
        "llm.temperature": {"value": 0.7, "ui_element": "range"},
        "search.tool": {"value": "searxng", "ui_element": "select"},
        "search.iterations": {"value": 2, "ui_element": "number"},
        "search.questions_per_iteration": {"value": 3, "ui_element": "number"},
        "search.max_results": {"value": 20, "ui_element": "number"},
    }


@pytest.fixture
def mock_get_llm(mock_llm):
    """Mock get_llm function."""
    with patch(
        "local_deep_research.api.research_functions.get_llm",
        return_value=mock_llm,
    ):
        yield mock_llm


@pytest.fixture
def mock_get_search(mock_search_engine):
    """Mock get_search function."""
    with patch(
        "local_deep_research.api.research_functions.get_search",
        return_value=mock_search_engine,
    ):
        yield mock_search_engine


@pytest.fixture
def mock_advanced_search_system(mock_search_system):
    """Mock AdvancedSearchSystem class."""
    with patch(
        "local_deep_research.api.research_functions.AdvancedSearchSystem",
        return_value=mock_search_system,
    ):
        yield mock_search_system
