"""Shared fixtures for integration tests that call real LLM APIs."""

import pytest
import pytest_asyncio

from dotpromptz.credentials import load_credentials

OPENAI_INTEGRATION_MODEL = 'gpt-5.2'
ANTHROPIC_INTEGRATION_MODEL = 'claude-haiku-4-5-20251001'
GOOGLE_INTEGRATION_MODEL = 'gemini-3-flash-preview'


def pytest_configure(config: pytest.Config) -> None:
    """Register the ``integration`` marker."""
    config.addinivalue_line(
        'markers',
        'integration: tests that call real LLM APIs (require API keys)',
    )


@pytest.fixture(autouse=True)
def _skip_without_credentials(request) -> None:
    """Automatically skip integration tests when no matching credentials are available.

    Checks the API_CREDENTIALS pool for credentials matching the adapter
    required by each test's fixture.
    """
    pool = load_credentials()
    fixture_names = getattr(request, 'fixturenames', [])
    if 'anthropic_adapter' in fixture_names:
        if not pool.filter('anthropic', model=ANTHROPIC_INTEGRATION_MODEL):
            pytest.skip(f'No anthropic credentials support model {ANTHROPIC_INTEGRATION_MODEL!r}')
    elif 'google_adapter' in fixture_names:
        if not pool.filter('google', model=GOOGLE_INTEGRATION_MODEL):
            pytest.skip(f'No google credentials support model {GOOGLE_INTEGRATION_MODEL!r}')
    else:
        if not pool.filter('openai', model=OPENAI_INTEGRATION_MODEL):
            pytest.skip(f'No openai credentials support model {OPENAI_INTEGRATION_MODEL!r}')


@pytest_asyncio.fixture()
async def openai_adapter():
    """Create an OpenAIAdapter with credentials from the pool."""
    from dotpromptz.adapters.openai import OpenAIAdapter

    pool = load_credentials()
    try:
        cred = pool.next('openai', model=OPENAI_INTEGRATION_MODEL)
    except ValueError:
        pytest.skip(f'No openai credentials support model {OPENAI_INTEGRATION_MODEL!r}')
    adapter = OpenAIAdapter(credential=cred, default_model=OPENAI_INTEGRATION_MODEL)
    yield adapter
    await adapter.aclose()


@pytest_asyncio.fixture()
async def anthropic_adapter():
    """Create an AnthropicAdapter with credentials from the pool."""
    from dotpromptz.adapters.anthropic import AnthropicAdapter

    pool = load_credentials()
    try:
        cred = pool.next('anthropic', model=ANTHROPIC_INTEGRATION_MODEL)
    except ValueError:
        pytest.skip(f'No anthropic credentials support model {ANTHROPIC_INTEGRATION_MODEL!r}')
    adapter = AnthropicAdapter(credential=cred, default_model=ANTHROPIC_INTEGRATION_MODEL)
    yield adapter
    await adapter.aclose()


@pytest_asyncio.fixture()
async def google_adapter():
    """Create a GoogleAdapter with credentials from the pool."""
    from dotpromptz.adapters.google import GoogleAdapter

    pool = load_credentials()
    try:
        cred = pool.next('google', model=GOOGLE_INTEGRATION_MODEL)
    except ValueError:
        pytest.skip(f'No google credentials support model {GOOGLE_INTEGRATION_MODEL!r}')
    adapter = GoogleAdapter(credential=cred, default_model=GOOGLE_INTEGRATION_MODEL)
    yield adapter
    await adapter.aclose()


@pytest.fixture()
def dotprompt_instance():
    """Create a vanilla Dotprompt instance."""
    from dotpromptz import Dotprompt

    return Dotprompt(default_model=OPENAI_INTEGRATION_MODEL)
