"""Step definitions for TDD test: ProviderType.GEMINI missing from FALLBACK_ORDER.

Issue: #4750 — ProviderType.GEMINI missing from ProviderRegistry.FALLBACK_ORDER
TDD Issue: #10896

This test captures the bug: when only GEMINI_API_KEY is set (without GOOGLE_API_KEY),
get_default_provider_type() returns None instead of ProviderType.GEMINI because
GEMINI is absent from FALLBACK_ORDER.

The @tdd_expected_fail tag inverts the result so CI passes while the bug exists.
Once the fix is applied (adding ProviderType.GEMINI to FALLBACK_ORDER), the
@tdd_expected_fail tag must be removed and the test must pass normally.
"""

from __future__ import annotations

import os
from typing import Any
from unittest.mock import MagicMock

from behave import given, then, when  # type: ignore[import-untyped]

from cleveragents.providers.registry import ProviderRegistry, ProviderType


def _make_gemini_only_settings() -> Any:
    """Return a fake Settings-like object with only gemini_api_key set."""
    settings = MagicMock()
    settings.openai_api_key = None
    settings.anthropic_api_key = None
    settings.google_api_key = None
    settings.gemini_api_key = "sk-gemini-test-key"
    settings.azure_api_key = None
    settings.openrouter_api_key = None
    settings.cohere_api_key = None
    settings.groq_api_key = None
    settings.together_api_key = None
    settings.default_provider = None
    settings.default_model = None
    settings.azure_openai_endpoint = None
    settings.azure_openai_api_version = None
    settings.azure_openai_deployment = None
    settings.openrouter_organization = None
    return settings


@given("a ProviderRegistry configured with only gemini_api_key set")
def step_tdd_registry_gemini_only(context: Any) -> None:
    context.tdd_registry = ProviderRegistry(settings=_make_gemini_only_settings())


@given("CLEVERAGENTS_DEFAULT_PROVIDER is not set for tdd test")
def step_tdd_unset_default_provider(context: Any) -> None:
    context.original_provider_env = os.environ.get("CLEVERAGENTS_DEFAULT_PROVIDER")
    os.environ.pop("CLEVERAGENTS_DEFAULT_PROVIDER", None)


@when("I call get_default_provider_type on the tdd registry")
def step_tdd_call_get_default_provider_type(context: Any) -> None:
    context.tdd_result = context.tdd_registry.get_default_provider_type()


@then("the tdd result should be ProviderType.GEMINI")
def step_tdd_result_should_be_gemini(context: Any) -> None:
    assert context.tdd_result == ProviderType.GEMINI, (
        f"Expected ProviderType.GEMINI but got {context.tdd_result!r}. "
        f"Bug: ProviderType.GEMINI is missing from ProviderRegistry.FALLBACK_ORDER. "
        f"When only GEMINI_API_KEY is set, get_default_provider_type() returns None "
        f"instead of ProviderType.GEMINI."
    )
