"""Tests for GAuthConfig."""

import pytest

from gnexus_gauth.config import GAuthConfig
from gnexus_gauth.exceptions import ConfigurationException


class TestGAuthConfig:
    def test_valid_config(self) -> None:
        config = GAuthConfig(
            base_url="https://auth.example.test",
            client_id="billing",
            client_secret="secret",
            redirect_uri="https://billing.example.test/callback",
        )
        assert config.base_url == "https://auth.example.test"
        assert config.client_id == "billing"
        assert config.client_secret == "secret"
        assert config.redirect_uri == "https://billing.example.test/callback"
        assert config.authorize_url == "https://auth.example.test/oauth/authorize"
        assert config.token_url == "https://auth.example.test/oauth/token"

    def test_base_url_trailing_slash_stripped(self) -> None:
        config = GAuthConfig(
            base_url="https://auth.example.test/",
            client_id="billing",
            client_secret="secret",
            redirect_uri="https://billing.example.test/callback",
        )
        assert config.base_url == "https://auth.example.test"

    def test_empty_base_url_raises(self) -> None:
        with pytest.raises(ConfigurationException, match="base URL"):
            GAuthConfig(
                base_url="",
                client_id="billing",
                client_secret="secret",
                redirect_uri="https://billing.example.test/callback",
            )

    def test_invalid_base_url_raises(self) -> None:
        with pytest.raises(ConfigurationException, match="base URL"):
            GAuthConfig(
                base_url="not-a-url",
                client_id="billing",
                client_secret="secret",
                redirect_uri="https://billing.example.test/callback",
            )

    def test_empty_client_id_raises(self) -> None:
        with pytest.raises(ConfigurationException, match="client_id"):
            GAuthConfig(
                base_url="https://auth.example.test",
                client_id="",
                client_secret="secret",
                redirect_uri="https://billing.example.test/callback",
            )

    def test_empty_client_secret_raises(self) -> None:
        with pytest.raises(ConfigurationException, match="client_secret"):
            GAuthConfig(
                base_url="https://auth.example.test",
                client_id="billing",
                client_secret="",
                redirect_uri="https://billing.example.test/callback",
            )

    def test_invalid_redirect_uri_raises(self) -> None:
        with pytest.raises(ConfigurationException, match="redirect URI"):
            GAuthConfig(
                base_url="https://auth.example.test",
                client_id="billing",
                client_secret="secret",
                redirect_uri="not-a-url",
            )

    def test_state_ttl_too_low_raises(self) -> None:
        with pytest.raises(ConfigurationException, match="TTL"):
            GAuthConfig(
                base_url="https://auth.example.test",
                client_id="billing",
                client_secret="secret",
                redirect_uri="https://billing.example.test/callback",
                state_ttl_seconds=30,
            )

    def test_negative_webhook_tolerance_raises(self) -> None:
        with pytest.raises(ConfigurationException, match="tolerance"):
            GAuthConfig(
                base_url="https://auth.example.test",
                client_id="billing",
                client_secret="secret",
                redirect_uri="https://billing.example.test/callback",
                webhook_tolerance_seconds=-1,
            )

    def test_custom_paths(self) -> None:
        config = GAuthConfig(
            base_url="https://auth.example.test",
            client_id="billing",
            client_secret="secret",
            redirect_uri="https://billing.example.test/callback",
            authorize_path="/auth",
            token_path="/token",
            refresh_path="/refresh",
            revoke_path="/revoke",
            user_info_path="/me",
        )
        assert config.authorize_url == "https://auth.example.test/auth"
        assert config.token_url == "https://auth.example.test/token"
        assert config.refresh_url == "https://auth.example.test/refresh"
        assert config.revoke_url == "https://auth.example.test/revoke"
        assert config.user_info_url == "https://auth.example.test/me"
