"""
Fixtures for MCP server tests.
"""

import pytest
from unittest.mock import patch


@pytest.fixture
def mock_quick_summary():
    """Mock the quick_summary function."""
    mock_result = {
        "summary": "This is a test summary about quantum computing.",
        "findings": [
            {
                "phase": "Iteration 1",
                "content": "Quantum computing uses qubits instead of classical bits.",
            }
        ],
        "iterations": 1,
        "questions": {0: ["What is quantum computing?"]},
        "formatted_findings": "## Findings\n\nQuantum computing uses qubits.",
        "sources": [
            {
                "title": "Wikipedia - Quantum Computing",
                "link": "https://en.wikipedia.org/wiki/Quantum_computing",
            }
        ],
    }
    with patch(
        "local_deep_research.mcp.server.ldr_quick_summary",
        return_value=mock_result,
    ) as mock:
        yield mock


@pytest.fixture
def mock_detailed_research():
    """Mock the detailed_research function."""
    mock_result = {
        "query": "quantum computing applications",
        "research_id": "test-research-123",
        "summary": "Detailed analysis of quantum computing applications.",
        "findings": [
            {
                "phase": "Iteration 1",
                "content": "Quantum computing has applications in cryptography.",
            },
            {
                "phase": "Iteration 2",
                "content": "Drug discovery is another major application.",
            },
        ],
        "iterations": 2,
        "questions": {
            0: ["What are quantum computing applications?"],
            1: ["How is it used in cryptography?"],
        },
        "formatted_findings": "## Detailed Findings\n\nCryptography and drug discovery.",
        "sources": [
            {
                "title": "Nature - Quantum Applications",
                "link": "https://nature.com/quantum",
            },
        ],
        "metadata": {
            "timestamp": "2024-01-15T10:30:00Z",
            "search_tool": "wikipedia",
            "strategy": "source-based",
        },
    }
    with patch(
        "local_deep_research.mcp.server.ldr_detailed_research",
        return_value=mock_result,
    ) as mock:
        yield mock


@pytest.fixture
def mock_generate_report():
    """Mock the generate_report function."""
    mock_result = {
        "content": "# Research Report\n\n## Introduction\n\nThis report covers...",
        "metadata": {
            "generated_at": "2024-01-15T10:30:00Z",
            "query": "quantum computing",
        },
    }
    with patch(
        "local_deep_research.mcp.server.ldr_generate_report",
        return_value=mock_result,
    ) as mock:
        yield mock


@pytest.fixture
def mock_analyze_documents():
    """Mock the analyze_documents function."""
    mock_result = {
        "summary": "Analysis of documents in the test collection.",
        "documents": [
            {
                "title": "Test Document 1",
                "content": "Content of test document 1.",
                "link": "/path/to/doc1.pdf",
            },
        ],
        "collection": "test_collection",
        "document_count": 1,
    }
    with patch(
        "local_deep_research.mcp.server.ldr_analyze_documents",
        return_value=mock_result,
    ) as mock:
        yield mock


@pytest.fixture
def mock_settings_snapshot():
    """Mock settings snapshot for configuration tests."""
    mock_settings = {
        "llm.provider": {"value": "openai"},
        "llm.model": {"value": "gpt-4"},
        "llm.temperature": {"value": 0.7},
        "search.tool": {"value": "searxng"},
        "search.search_strategy": {"value": "source-based"},
        "search.iterations": {"value": 2},
        "search.questions_per_iteration": {"value": 3},
        "search.max_results": {"value": 10},
    }
    with patch(
        "local_deep_research.mcp.server.create_settings_snapshot",
        return_value=mock_settings,
    ) as mock:
        yield mock


# =============================================================================
# Additional fixtures for edge case and integration tests
# =============================================================================


@pytest.fixture
def sample_long_query():
    """Generate a very long query string (10000+ chars)."""
    return (
        "What is quantum computing and how does it work? " * 250
    )  # ~12500 chars


@pytest.fixture
def sample_special_chars_query():
    """Query with unicode, emojis, special characters."""
    return "What is 量子计算? 🔬 Test with émojis & spëcial <chars> \"quotes\" 'apostrophes'"


@pytest.fixture
def mock_api_empty_response():
    """Mock API response with missing fields."""
    return {}


@pytest.fixture
def mock_api_minimal_response():
    """Mock API response with only required fields."""
    return {
        "summary": "Minimal response",
    }


@pytest.fixture
def mock_api_with_nulls():
    """Mock API response with None values."""
    return {
        "summary": None,
        "findings": None,
        "sources": None,
        "iterations": None,
    }


@pytest.fixture
def mock_comprehensive_research_result():
    """Comprehensive mock research result for integration tests."""
    return {
        "query": "test query",
        "research_id": "integration-test-123",
        "summary": "This is a comprehensive test summary with multiple findings.",
        "findings": [
            {"phase": "Iteration 1", "content": "First finding content"},
            {"phase": "Iteration 2", "content": "Second finding content"},
            {"phase": "Iteration 3", "content": "Third finding content"},
        ],
        "iterations": 3,
        "questions": {
            0: ["Question 1?", "Question 2?"],
            1: ["Follow-up 1?"],
            2: ["Final question?"],
        },
        "formatted_findings": "## Research Findings\n\n- Finding 1\n- Finding 2\n- Finding 3",
        "sources": [
            {"title": "Source 1", "link": "https://example.com/1"},
            {"title": "Source 2", "link": "https://example.com/2"},
            {"title": "Source 3", "link": "https://example.com/3"},
        ],
        "metadata": {
            "timestamp": "2024-01-15T12:00:00Z",
            "search_tool": "searxng",
            "strategy": "source-based",
            "duration_seconds": 120,
        },
    }
