"""Tests for vmcluster_mcp.config — dual-source config loading."""

from vmcluster_mcp.config import load_config


class TestConfigDefaults:
    def test_defaults_no_file_no_env(self, monkeypatch, tmp_path):
        monkeypatch.setenv("VMCLUSTER_CONFIG", str(tmp_path / "nonexistent.yaml"))
        for key in (
            "VMCLUSTER_TOPOLOGY_DIR",
            "VMCLUSTER_OVERLAY_DIR",
            "VMCLUSTER_ARTIFACT_REGISTRY",
            "VMCLUSTER_ARTIFACT_STORE_DIR",
            "VMCLUSTER_SSH_KEY_PATH",
            "VMCLUSTER_SSH_USER",
            "VMCLUSTER_LIBVIRT_URI",
            "VMCLUSTER_LOG_LEVEL",
        ):
            monkeypatch.delenv(key, raising=False)

        cfg = load_config()
        assert cfg.topology_dir == "/etc/vmcluster/topologies"
        assert cfg.libvirt_uri == "qemu:///system"
        assert cfg.ssh_user == "root"
        assert cfg.log_level == "INFO"


class TestConfigEnvVars:
    def test_env_var_overrides_default(self, monkeypatch, tmp_path):
        monkeypatch.setenv("VMCLUSTER_CONFIG", str(tmp_path / "nonexistent.yaml"))
        monkeypatch.setenv("VMCLUSTER_TOPOLOGY_DIR", "/custom/topologies")
        cfg = load_config()
        assert cfg.topology_dir == "/custom/topologies"

    def test_env_var_overrides_file(self, monkeypatch, tmp_path):
        config_file = tmp_path / "config.yaml"
        config_file.write_text("topology_dir: /from/file\n")
        monkeypatch.setenv("VMCLUSTER_CONFIG", str(config_file))
        monkeypatch.setenv("VMCLUSTER_TOPOLOGY_DIR", "/from/env")
        cfg = load_config()
        assert cfg.topology_dir == "/from/env"


class TestConfigFile:
    def test_loads_from_yaml_file(self, monkeypatch, tmp_path):
        config_file = tmp_path / "config.yaml"
        config_file.write_text(
            "topology_dir: /my/topologies\n"
            "overlay_dir: /my/overlays\n"
            "libvirt_uri: qemu+ssh://remote/system\n"
        )
        monkeypatch.setenv("VMCLUSTER_CONFIG", str(config_file))
        monkeypatch.delenv("VMCLUSTER_TOPOLOGY_DIR", raising=False)
        monkeypatch.delenv("VMCLUSTER_OVERLAY_DIR", raising=False)
        monkeypatch.delenv("VMCLUSTER_LIBVIRT_URI", raising=False)
        cfg = load_config()
        assert cfg.topology_dir == "/my/topologies"
        assert cfg.overlay_dir == "/my/overlays"
        assert cfg.libvirt_uri == "qemu+ssh://remote/system"

    def test_vmcluster_config_env_specifies_path(self, monkeypatch, tmp_path):
        config_file = tmp_path / "custom.yaml"
        config_file.write_text("log_level: DEBUG\n")
        monkeypatch.setenv("VMCLUSTER_CONFIG", str(config_file))
        monkeypatch.delenv("VMCLUSTER_LOG_LEVEL", raising=False)
        cfg = load_config()
        assert cfg.log_level == "DEBUG"

    def test_empty_file_returns_defaults(self, monkeypatch, tmp_path):
        config_file = tmp_path / "empty.yaml"
        config_file.write_text("")
        monkeypatch.setenv("VMCLUSTER_CONFIG", str(config_file))
        cfg = load_config()
        assert cfg.topology_dir == "/etc/vmcluster/topologies"
