"""Unit tests for ConfigManager."""

import pytest

from ai_org.core.config_manager import ConfigManager


@pytest.mark.unit
def test_default_config_path():
    """Default config path should be ~/.ai-org.env."""
    mgr = ConfigManager()
    assert mgr.config_path.name == ".ai-org.env"
    assert str(mgr.config_path.parent).endswith(str(mgr.config_path.parent.name))


@pytest.mark.unit
def test_custom_config_path(tmp_path):
    """ConfigManager should accept a custom config path."""
    custom = tmp_path / "custom.env"
    mgr = ConfigManager(config_path=custom)
    assert mgr.config_path == custom


@pytest.mark.unit
def test_load_defaults(tmp_path, monkeypatch):
    """load() should return DEFAULTS when no file exists and no env vars are set."""
    # Clear any env vars that could interfere
    for key in ConfigManager.DEFAULTS:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.delenv("DEFAULT_SSO_USER", raising=False)
    monkeypatch.delenv("NOTIFICATIONS_EMAIL", raising=False)

    config_file = tmp_path / "nonexistent.env"
    mgr = ConfigManager(config_path=config_file)
    config = mgr.load()

    for key, value in ConfigManager.DEFAULTS.items():
        assert config[key] == value


@pytest.mark.unit
def test_load_from_file(tmp_path, monkeypatch):
    """load() should read values from a config file."""
    for key in ConfigManager.DEFAULTS:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.delenv("DEFAULT_SSO_USER", raising=False)
    monkeypatch.delenv("NOTIFICATIONS_EMAIL", raising=False)

    config_file = tmp_path / "test.env"
    config_file.write_text("AWS_PROFILE=custom-profile\n")
    mgr = ConfigManager(config_path=config_file)
    config = mgr.load()
    assert config["AWS_PROFILE"] == "custom-profile"


@pytest.mark.unit
def test_load_env_overrides_file(tmp_path, monkeypatch):
    """Environment variables should take precedence over file values."""
    for key in ConfigManager.DEFAULTS:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.delenv("DEFAULT_SSO_USER", raising=False)
    monkeypatch.delenv("NOTIFICATIONS_EMAIL", raising=False)

    config_file = tmp_path / "test.env"
    config_file.write_text("AWS_PROFILE=from-file\n")
    monkeypatch.setenv("AWS_PROFILE", "from-env")

    mgr = ConfigManager(config_path=config_file)
    config = mgr.load()
    assert config["AWS_PROFILE"] == "from-env"


@pytest.mark.unit
def test_get_with_default(tmp_path, monkeypatch):
    """get() should return the provided default when a key is missing."""
    for key in ConfigManager.DEFAULTS:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.delenv("DEFAULT_SSO_USER", raising=False)
    monkeypatch.delenv("NOTIFICATIONS_EMAIL", raising=False)

    config_file = tmp_path / "test.env"
    mgr = ConfigManager(config_path=config_file)
    assert mgr.get("NONEXISTENT_KEY", "fallback") == "fallback"


@pytest.mark.unit
def test_set_creates_file(tmp_path):
    """set() should create the config file if it does not exist."""
    config_file = tmp_path / "subdir" / "new.env"
    mgr = ConfigManager(config_path=config_file)
    mgr.set("MY_KEY", "my_value")
    assert config_file.exists()


@pytest.mark.unit
def test_set_writes_value(tmp_path, monkeypatch):
    """set() should persist a key=value pair in the config file."""
    for key in ConfigManager.DEFAULTS:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.delenv("DEFAULT_SSO_USER", raising=False)
    monkeypatch.delenv("NOTIFICATIONS_EMAIL", raising=False)

    config_file = tmp_path / "test.env"
    mgr = ConfigManager(config_path=config_file)
    mgr.set("CUSTOM_KEY", "custom_value")

    content = config_file.read_text()
    assert "CUSTOM_KEY" in content
    assert "custom_value" in content


@pytest.mark.unit
def test_exists_true(tmp_path):
    """exists() should return True when the config file is present."""
    config_file = tmp_path / "test.env"
    config_file.write_text("")
    mgr = ConfigManager(config_path=config_file)
    assert mgr.exists() is True


@pytest.mark.unit
def test_exists_false(tmp_path):
    """exists() should return False when the config file is absent."""
    config_file = tmp_path / "missing.env"
    mgr = ConfigManager(config_path=config_file)
    assert mgr.exists() is False


@pytest.mark.unit
def test_delete(tmp_path):
    """delete() should remove the config file."""
    config_file = tmp_path / "test.env"
    config_file.write_text("KEY=value\n")
    mgr = ConfigManager(config_path=config_file)
    mgr.delete()
    assert not config_file.exists()


@pytest.mark.unit
def test_get_aws_profile_default(tmp_path, monkeypatch):
    """get_aws_profile() should return 'default' when unconfigured."""
    for key in ConfigManager.DEFAULTS:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.delenv("DEFAULT_SSO_USER", raising=False)
    monkeypatch.delenv("NOTIFICATIONS_EMAIL", raising=False)

    mgr = ConfigManager(config_path=tmp_path / "empty.env")
    assert mgr.get_aws_profile() == "default"


@pytest.mark.unit
def test_get_monthly_budget_default(tmp_path, monkeypatch):
    """get_monthly_budget() should return 1000 when unconfigured."""
    for key in ConfigManager.DEFAULTS:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.delenv("DEFAULT_SSO_USER", raising=False)
    monkeypatch.delenv("NOTIFICATIONS_EMAIL", raising=False)

    mgr = ConfigManager(config_path=tmp_path / "empty.env")
    assert mgr.get_monthly_budget() == 1000


@pytest.mark.unit
def test_get_anomaly_threshold_default(tmp_path, monkeypatch):
    """get_anomaly_threshold() should return 100 when unconfigured."""
    for key in ConfigManager.DEFAULTS:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.delenv("DEFAULT_SSO_USER", raising=False)
    monkeypatch.delenv("NOTIFICATIONS_EMAIL", raising=False)

    mgr = ConfigManager(config_path=tmp_path / "empty.env")
    assert mgr.get_anomaly_threshold() == 100


@pytest.mark.unit
def test_get_default_permission_set(tmp_path, monkeypatch):
    """get_default_permission_set() should return 'AWSAdministratorAccess'."""
    for key in ConfigManager.DEFAULTS:
        monkeypatch.delenv(key, raising=False)
    monkeypatch.delenv("DEFAULT_SSO_USER", raising=False)
    monkeypatch.delenv("NOTIFICATIONS_EMAIL", raising=False)

    mgr = ConfigManager(config_path=tmp_path / "empty.env")
    assert mgr.get_default_permission_set() == "AWSAdministratorAccess"
