"""Tests for ai4gh.core.config."""

from __future__ import annotations

from pathlib import Path

import pytest

from ai4gh.core.config import AuthConfig, Config


class TestAuthConfig:
    def test_defaults(self) -> None:
        auth = AuthConfig()
        assert auth.token is None
        assert auth.token_url is None
        assert auth.client_id is None
        assert auth.client_secret is None
        assert auth.passport_broker is None

    def test_with_token(self) -> None:
        auth = AuthConfig(token="test-token")
        assert auth.token == "test-token"


class TestConfig:
    def test_defaults(self) -> None:
        config = Config()
        assert config.base_url is None
        assert config.timeout == 30.0
        assert config.max_retries == 3
        assert config.verify_ssl is True
        assert config.log_level == "INFO"

    def test_custom_values(self) -> None:
        config = Config(
            base_url="https://example.org",
            timeout=10.0,
            max_retries=5,
        )
        assert config.base_url == "https://example.org"
        assert config.timeout == 10.0
        assert config.max_retries == 5

    def test_from_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("AI4GH_BASE_URL", "https://test.example.org")
        monkeypatch.setenv("AI4GH_TIMEOUT", "15")
        monkeypatch.setenv("AI4GH_MAX_RETRIES", "5")
        monkeypatch.setenv("AI4GH_AUTH_TOKEN", "test-token-123")

        config = Config.from_env()
        assert config.base_url == "https://test.example.org"
        assert config.timeout == 15.0
        assert config.max_retries == 5
        assert config.auth.token == "test-token-123"

    def test_from_env_verify_ssl_false(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("AI4GH_VERIFY_SSL", "false")
        config = Config.from_env()
        assert config.verify_ssl is False

    def test_from_file_nonexistent(self, tmp_path: Path) -> None:
        config = Config.from_file(tmp_path / "nonexistent.toml")
        assert config.base_url is None

    def test_from_file(self, tmp_path: Path) -> None:
        config_file = tmp_path / "config.toml"
        config_file.write_text(
            'base_url = "https://from-file.example.org"\n'
            "timeout = 20.0\n\n"
            '[auth]\ntoken = "file-token"\n'
        )
        config = Config.from_file(config_file)
        assert config.base_url == "https://from-file.example.org"
        assert config.timeout == 20.0
        assert config.auth.token == "file-token"

    def test_get_spec_url_from_env(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("AI4GH_BEACON_URL", "https://beacon.example.org")
        config = Config(base_url="https://default.example.org")
        assert config.get_spec_url("beacon") == "https://beacon.example.org"

    def test_get_spec_url_falls_back_to_base(self) -> None:
        config = Config(base_url="https://default.example.org")
        assert config.get_spec_url("beacon") == "https://default.example.org"

    def test_get_spec_url_none_when_no_config(self) -> None:
        config = Config()
        assert config.get_spec_url("beacon") is None

    def test_from_env_profile(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("AI4GH_PROFILE", "synapse")
        config = Config.from_env()
        assert config.profile == "synapse"

    def test_from_env_log_level(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("AI4GH_LOG_LEVEL", "DEBUG")
        config = Config.from_env()
        assert config.log_level == "DEBUG"

    def test_from_env_auth_token_url(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("AI4GH_AUTH_TOKEN_URL", "https://auth.example.org/token")
        monkeypatch.setenv("AI4GH_AUTH_CLIENT_ID", "client-1")
        monkeypatch.setenv("AI4GH_AUTH_CLIENT_SECRET", "secret-1")
        config = Config.from_env()
        assert config.auth.token_url == "https://auth.example.org/token"
        assert config.auth.client_id == "client-1"
        assert config.auth.client_secret == "secret-1"

    def test_from_env_passport_broker(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.setenv("AI4GH_PASSPORT_BROKER", "https://broker.example.org")
        config = Config.from_env()
        assert config.auth.passport_broker == "https://broker.example.org"

    def test_from_file_without_auth(self, tmp_path: Path) -> None:
        config_file = tmp_path / "config.toml"
        config_file.write_text('base_url = "https://no-auth.example.org"\n')
        config = Config.from_file(config_file)
        assert config.base_url == "https://no-auth.example.org"
        assert config.auth.token is None

    def test_load_defaults(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
        # Point to nonexistent config file so load() uses defaults
        monkeypatch.setattr("ai4gh.core.config.Path", lambda p: tmp_path / "nonexistent")
        config = Config.load()
        assert isinstance(config, Config)

    def test_load_with_overrides(self, monkeypatch: pytest.MonkeyPatch) -> None:
        monkeypatch.delenv("AI4GH_BASE_URL", raising=False)
        config = Config.load(base_url="https://override.example.org", timeout=42.0)
        assert config.base_url == "https://override.example.org"
        assert config.timeout == 42.0
