"""Shared fixtures for metrics tests."""

from unittest.mock import MagicMock, Mock, patch

import pytest


@pytest.fixture
def mock_flask_session():
    """Mock Flask session with username."""
    mock_session = {"username": "testuser", "session_id": "test-session-id"}
    with patch("flask.session", mock_session):
        yield mock_session


@pytest.fixture
def mock_db_session():
    """Mock database session."""
    session = MagicMock()
    session.query.return_value.filter.return_value.all.return_value = []
    session.query.return_value.filter.return_value.first.return_value = None
    session.query.return_value.filter_by.return_value.all.return_value = []
    session.query.return_value.filter_by.return_value.first.return_value = None
    return session


@pytest.fixture
def mock_search_context():
    """Mock search context for SearchTracker."""
    return {
        "research_id": "test-uuid-123",
        "research_query": "test query",
        "research_mode": "quick",
        "research_phase": "search",
        "search_iteration": 1,
        "username": "testuser",
        "user_password": "testpass",
    }


@pytest.fixture
def mock_token_usage_data():
    """Sample token usage data for testing."""
    return {
        "model_name": "gpt-4",
        "model_provider": "openai",
        "prompt_tokens": 100,
        "completion_tokens": 50,
        "total_tokens": 150,
        "research_id": "test-uuid-123",
    }


@pytest.fixture
def mock_llm_response():
    """Mock LangChain LLM response."""
    response = Mock()
    response.llm_output = {
        "token_usage": {
            "prompt_tokens": 100,
            "completion_tokens": 50,
            "total_tokens": 150,
        }
    }
    response.generations = []
    return response


@pytest.fixture
def mock_metrics_writer():
    """Mock thread-safe metrics writer."""
    writer = MagicMock()
    writer.get_session.return_value.__enter__ = Mock(return_value=MagicMock())
    writer.get_session.return_value.__exit__ = Mock(return_value=None)
    return writer
