"""Shared pytest fixtures and configuration."""

import pytest

from ai_qa.core.config import reset_config
from ai_qa.core.context import Context


@pytest.fixture(autouse=True)
def reset_config_fixture():
    """Reset config before each test to ensure isolation."""
    from ai_qa.providers import ProviderRegistry

    reset_config()
    ProviderRegistry.set_instance(None)
    yield
    reset_config()
    ProviderRegistry.set_instance(None)


@pytest.fixture
def sample_context():
    """Create a sample context for testing."""
    return Context(
        input="What is 2+2?",
        output="4",
        ground_truth="4",
        context=["Basic arithmetic: 2+2=4"],
        metadata={"model": "test-model"},
    )


@pytest.fixture
def minimal_context():
    """Create a minimal context with only output."""
    return Context(output="test output")


@pytest.fixture
def rag_context():
    """Create a context typical for RAG systems."""
    return Context(
        input="What is the capital of France?",
        output="Paris",
        context=[
            "Paris is the capital and largest city of France.",
            "It is located in northern France on the Seine River.",
        ],
        ground_truth="Paris",
        metadata={"retrieval_method": "bm25"},
    )


@pytest.fixture
def conversation_context():
    """Create a context typical for multi-turn conversations."""
    return Context(
        output="The answer is 42.",
        conversation=[
            {"role": "user", "content": "What is the answer to life?"},
            {
                "role": "assistant",
                "content": "The answer to life, the universe, and everything is 42.",
            },
            {"role": "user", "content": "Why 42?"},
            {"role": "assistant", "content": "The answer is 42."},
        ],
        metadata={"num_turns": 2},
    )


@pytest.fixture
def agent_context():
    """Create a context typical for agent workflows."""
    return Context(
        input="Book a flight from NYC to LA on Dec 25",
        output="Flight booked successfully",
        tool_calls=[
            {"name": "search_flights", "arguments": {"from": "NYC", "to": "LA"}},
            {
                "name": "book_flight",
                "arguments": {
                    "flight_id": "AA123",
                    "date": "2024-12-25",
                },
            },
        ],
        metadata={"agent_name": "flight_agent"},
    )


@pytest.fixture
def mock_provider():
    """Create a mock provider for testing."""

    from ai_qa.providers.base import BaseProvider

    class MockProvider(BaseProvider):
        """Mock provider for testing."""

        name = "mock"

        def __init__(self, model: str = "mock-model", **kwargs):
            super().__init__(model, **kwargs)

        def judge(self, system: str, user: str):
            return {
                "score": 0.8,
                "evidence": "Mock judgment",
                "cost_usd": 0.01,
                "tokens": {"input": 100, "output": 50},
            }

        def generate(self, prompt: str, system: str | None = None):
            return "Mock generated text"

    return MockProvider()
