"""Tests for configuration management."""

import pytest
from pathlib import Path
import tempfile
import os

from fomo_guard.config import Config, APIKeys, Settings


class TestConfig:
    """Tests for Config class."""

    def test_default_config(self):
        """Test default configuration values."""
        config = Config()

        assert config.api_keys.coingecko is None
        assert config.settings.default_cooldown_hours == 24
        assert config.settings.cache_ttl_minutes == 5

    def test_api_keys_model(self):
        """Test APIKeys model."""
        keys = APIKeys(
            coingecko="test-key",
            twitter="twitter-key",
        )

        assert keys.coingecko == "test-key"
        assert keys.twitter == "twitter-key"
        assert keys.glassnode is None

    def test_settings_model(self):
        """Test Settings model."""
        settings = Settings(
            default_cooldown_hours=48,
            api_port=9000,
        )

        assert settings.default_cooldown_hours == 48
        assert settings.api_port == 9000
        assert settings.cache_ttl_minutes == 5  # Default

    def test_mask_key_short(self):
        """Test masking short API keys."""
        masked = Config._mask_key("12345678")
        assert masked == "********"

    def test_mask_key_long(self):
        """Test masking long API keys."""
        masked = Config._mask_key("1234567890abcdef")
        assert masked.startswith("1234")
        assert masked.endswith("cdef")
        assert "*" in masked

    def test_get_api_key(self):
        """Test getting API key."""
        config = Config(
            api_keys=APIKeys(coingecko="test-key")
        )

        assert config.get_api_key("coingecko") == "test-key"
        assert config.get_api_key("twitter") is None

    def test_list_api_keys(self):
        """Test listing API keys."""
        config = Config(
            api_keys=APIKeys(
                coingecko="test-key-1234567890",
                twitter="another-key",
            )
        )

        keys = config.list_api_keys()

        assert "coingecko" in keys
        assert "twitter" in keys
        assert keys["coingecko"] is not None
        assert keys["glassnode"] is None


class TestConfigPersistence:
    """Tests for config file persistence."""

    def test_save_and_load(self, tmp_path, monkeypatch):
        """Test saving and loading config."""
        # Override config directory
        config_dir = tmp_path / ".config" / "fomo_guard"

        monkeypatch.setattr(
            Config, "get_config_dir",
            classmethod(lambda cls: config_dir)
        )
        monkeypatch.setattr(
            Config, "get_config_path",
            classmethod(lambda cls: config_dir / "config.toml")
        )

        # Create and save config
        config = Config(
            api_keys=APIKeys(coingecko="my-api-key"),
            settings=Settings(default_cooldown_hours=48),
        )
        config.save()

        # Load config
        loaded = Config.load()

        assert loaded.api_keys.coingecko == "my-api-key"
        assert loaded.settings.default_cooldown_hours == 48
