"""Unit tests for the OAuth provider builder."""

from ims_mcp.auth.oauth import build_oauth_provider
from ims_mcp.config import RosettaConfig


def _make_config(**overrides) -> RosettaConfig:
    defaults = dict(
        server_url="http://localhost:80",
        version="r1",
        api_key="test-key",
        posthog_api_key="",
        posthog_host="https://us.i.posthog.com",
        debug=False,
        root_filter=[],
        transport="stdio",
        http_host="0.0.0.0",
        http_port=8000,
        redis_url=None,
        allowed_origins=[],
        oauth_authorization_endpoint="",
        oauth_token_endpoint="",
        oauth_introspection_endpoint="",
        oauth_client_id="",
        oauth_client_secret="",
        oauth_base_url="",
        oauth_callback_path="/auth/callback",
        read_policy="all",
        write_policy="all",
        user_email="rosetta@griddynamics.net",
        invite_emails=[],
    )
    defaults.update(overrides)
    return RosettaConfig(**defaults)


def test_returns_none_for_stdio():
    cfg = _make_config(transport="stdio")
    assert build_oauth_provider(cfg) is None


def test_returns_none_when_oauth_incomplete():
    cfg = _make_config(
        transport="http",
        oauth_authorization_endpoint="https://kc.example.com/auth",
        # missing token/introspection/client_id/client_secret
    )
    assert build_oauth_provider(cfg) is None


def test_returns_none_when_all_empty():
    cfg = _make_config(transport="http")
    assert build_oauth_provider(cfg) is None


def test_returns_provider_when_fully_configured():
    cfg = _make_config(
        transport="http",
        oauth_authorization_endpoint="https://kc.example.com/auth",
        oauth_token_endpoint="https://kc.example.com/token",
        oauth_introspection_endpoint="https://kc.example.com/introspect",
        oauth_client_id="my-client",
        oauth_client_secret="my-secret",
        oauth_base_url="https://rosetta.example.com",
    )
    provider = build_oauth_provider(cfg)
    assert provider is not None


def test_oauth_configured_property():
    cfg = _make_config(
        oauth_authorization_endpoint="https://kc/auth",
        oauth_token_endpoint="https://kc/token",
        oauth_introspection_endpoint="https://kc/introspect",
        oauth_client_id="cid",
        oauth_client_secret="csec",
    )
    assert cfg.oauth_configured is True

    cfg2 = _make_config()
    assert cfg2.oauth_configured is False


def test_resolve_oauth_base_url_from_env():
    cfg = _make_config(oauth_base_url="https://rosetta.example.com/")
    assert cfg.resolve_oauth_base_url() == "https://rosetta.example.com"


def test_resolve_oauth_base_url_from_host_port():
    cfg = _make_config(http_host="0.0.0.0", http_port=9000)
    assert cfg.resolve_oauth_base_url() == "http://0.0.0.0:9000"


def test_uses_callback_path_from_config():
    cfg = _make_config(
        transport="http",
        oauth_authorization_endpoint="https://kc.example.com/auth",
        oauth_token_endpoint="https://kc.example.com/token",
        oauth_introspection_endpoint="https://kc.example.com/introspect",
        oauth_client_id="my-client",
        oauth_client_secret="my-secret",
        oauth_callback_path="oauth/cb",
    )
    provider = build_oauth_provider(cfg)
    assert provider is not None
    assert getattr(provider, "_redirect_path", None) == "/oauth/cb"
