# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

import os

import pytest
from openai import OpenAI

from llama_verifications.cli.load_provider_confs import (
    ProviderConfig,
    load_provider_configs,
)


@pytest.fixture(scope="session")
def verification_config() -> dict[str, ProviderConfig]:
    return load_provider_configs()


@pytest.fixture
def provider(request, verification_config: dict[str, ProviderConfig]):
    """Determines the provider based on CLI args and loaded configs."""
    provider_opt = request.config.getoption("--provider")
    base_url_opt = request.config.getoption("--base-url")

    available_providers = sorted(verification_config.keys())

    final_provider = None

    if provider_opt:
        if provider_opt not in available_providers:
            raise KeyError(
                f"Provider '{provider_opt}' (from --provider) not found in loaded configs. Available: {available_providers}"
            )

        # Optional: Check if base_url is consistent if both are provided
        if base_url_opt:
            cfg = verification_config.get(provider_opt)
            if not cfg:
                raise ValueError(f"Internal Error: No config found for validated provider '{provider_opt}'.")
            provider_base_url = cfg.base_url

            if provider_base_url != base_url_opt:
                raise ValueError(
                    f"Provided base URL '{base_url_opt}' (from --base-url) does not match configured base URL '{provider_base_url}' for provider '{provider_opt}'."
                )
        final_provider = provider_opt

    elif base_url_opt:
        return None
    else:
        pytest.fail("Cannot determine provider: Neither --provider nor --base-url specified.")

    if final_provider is None:
        # This case should ideally be unreachable due to the logic above
        pytest.fail("Internal Error: Failed to determine final provider.")

    return final_provider


@pytest.fixture
def base_url(request, provider, verification_config):
    return request.config.getoption("--base-url") or verification_config[provider].base_url


@pytest.fixture
def api_key(
    request,
    provider: str,
    verification_config: dict[str, ProviderConfig],
):
    if provider in verification_config:
        api_key_env_var = verification_config[provider].api_key_var
        if api_key_env_var == "DUMMY_API_KEY":
            return "foo"
    else:
        api_key_env_var = "OPENAI_API_KEY"

    key_from_option = request.config.getoption("--api-key")
    key_from_env = os.getenv(api_key_env_var) if api_key_env_var else None
    if not key_from_env and not key_from_option:
        raise ValueError(f"API key not found in environment variable {api_key_env_var} or --api-key option.")
    final_key = key_from_option or key_from_env
    return final_key


@pytest.fixture
def openai_client(base_url, api_key):
    """Pytest fixture to provide an initialized OpenAI client."""
    return OpenAI(base_url=base_url, api_key=api_key)


@pytest.fixture
def model(request):
    """Pytest fixture to provide the model name selected via --model."""
    return request.config.getoption("--model")
