import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

import pytest


class TestConfigTokenLoading:
    """환경변수에서 토큰을 정상 로드하는지 테스트"""

    def test_gemini_bot_token_loaded_from_env(self, monkeypatch):
        """GEMINI_BOT_TOKEN 환경변수가 설정되어 있으면 config에서 정상 로드되어야 한다"""
        monkeypatch.setenv("GEMINI_BOT_TOKEN", "test-gemini-bot-token-123")
        monkeypatch.setenv("CODEX_BOT_TOKEN", "test-codex-bot-token-456")
        monkeypatch.setenv("CLAUDE_BOT_TOKEN", "test-claude-bot-token-789")

        if "config" in sys.modules:
            del sys.modules["config"]

        from config import GEMINI_BOT_TOKEN

        assert GEMINI_BOT_TOKEN == "test-gemini-bot-token-123"

    def test_codex_bot_token_loaded_from_env(self, monkeypatch):
        """CODEX_BOT_TOKEN 환경변수가 설정되어 있으면 config에서 정상 로드되어야 한다"""
        monkeypatch.setenv("GEMINI_BOT_TOKEN", "test-gemini-bot-token-123")
        monkeypatch.setenv("CODEX_BOT_TOKEN", "test-codex-bot-token-456")
        monkeypatch.setenv("CLAUDE_BOT_TOKEN", "test-claude-bot-token-789")

        if "config" in sys.modules:
            del sys.modules["config"]

        from config import CODEX_BOT_TOKEN

        assert CODEX_BOT_TOKEN == "test-codex-bot-token-456"


class TestConfigDefaults:
    """기본값 설정 테스트"""

    def test_owner_user_id_default(self, monkeypatch):
        """OWNER_USER_ID 기본값이 올바르게 설정되어야 한다"""
        monkeypatch.setenv("GEMINI_BOT_TOKEN", "dummy-token")
        monkeypatch.setenv("CODEX_BOT_TOKEN", "dummy-token")
        monkeypatch.setenv("CLAUDE_BOT_TOKEN", "dummy-token")

        if "config" in sys.modules:
            del sys.modules["config"]

        from config import OWNER_USER_ID

        # 기본값은 정수형이어야 한다
        assert isinstance(OWNER_USER_ID, int)
        assert OWNER_USER_ID > 0

    def test_cli_timeout_default(self, monkeypatch):
        """CLI_TIMEOUT 기본값이 올바르게 설정되어야 한다"""
        monkeypatch.setenv("GEMINI_BOT_TOKEN", "dummy-token")
        monkeypatch.setenv("CODEX_BOT_TOKEN", "dummy-token")
        monkeypatch.setenv("CLAUDE_BOT_TOKEN", "dummy-token")

        if "config" in sys.modules:
            del sys.modules["config"]

        from config import CLI_TIMEOUT

        # 기본값은 양의 정수(초 단위)여야 한다
        assert isinstance(CLI_TIMEOUT, int)
        assert CLI_TIMEOUT > 0

    def test_max_message_length_default(self, monkeypatch):
        """MAX_MESSAGE_LENGTH 기본값이 텔레그램 제한인 4096이어야 한다"""
        monkeypatch.setenv("GEMINI_BOT_TOKEN", "dummy-token")
        monkeypatch.setenv("CODEX_BOT_TOKEN", "dummy-token")
        monkeypatch.setenv("CLAUDE_BOT_TOKEN", "dummy-token")

        if "config" in sys.modules:
            del sys.modules["config"]

        from config import MAX_MESSAGE_LENGTH

        assert MAX_MESSAGE_LENGTH == 4096

    def test_cli_timeout_is_positive_integer(self, monkeypatch):
        """CLI_TIMEOUT은 양의 정수여야 한다"""
        monkeypatch.setenv("GEMINI_BOT_TOKEN", "dummy-token")
        monkeypatch.setenv("CODEX_BOT_TOKEN", "dummy-token")
        monkeypatch.setenv("CLAUDE_BOT_TOKEN", "dummy-token")

        if "config" in sys.modules:
            del sys.modules["config"]

        from config import CLI_TIMEOUT

        assert CLI_TIMEOUT >= 1

    def test_owner_user_id_is_integer(self, monkeypatch):
        """OWNER_USER_ID는 정수형이어야 한다"""
        monkeypatch.setenv("GEMINI_BOT_TOKEN", "dummy-token")
        monkeypatch.setenv("CODEX_BOT_TOKEN", "dummy-token")
        monkeypatch.setenv("CLAUDE_BOT_TOKEN", "dummy-token")

        if "config" in sys.modules:
            del sys.modules["config"]

        from config import OWNER_USER_ID

        assert isinstance(OWNER_USER_ID, int)


class TestConfigMissingEnvVars:
    """필수 환경변수 누락 시 에러 발생 테스트"""

    def test_missing_gemini_bot_token_raises_key_error(self, monkeypatch):
        """GEMINI_BOT_TOKEN 환경변수가 없으면 KeyError가 발생해야 한다"""
        monkeypatch.delenv("GEMINI_BOT_TOKEN", raising=False)
        monkeypatch.delenv("CODEX_BOT_TOKEN", raising=False)
        monkeypatch.delenv("CLAUDE_BOT_TOKEN", raising=False)

        if "config" in sys.modules:
            del sys.modules["config"]

        with pytest.raises(KeyError):
            import config  # noqa: F401

    def test_missing_codex_bot_token_raises_key_error(self, monkeypatch):
        """CODEX_BOT_TOKEN 환경변수가 없으면 KeyError가 발생해야 한다"""
        monkeypatch.setenv("GEMINI_BOT_TOKEN", "dummy-token")
        monkeypatch.setenv("CLAUDE_BOT_TOKEN", "dummy-token")
        monkeypatch.delenv("CODEX_BOT_TOKEN", raising=False)

        if "config" in sys.modules:
            del sys.modules["config"]

        with pytest.raises(KeyError):
            import config  # noqa: F401
