"""Unit tests for config.py settings loading."""

import os
from unittest.mock import patch

from media_library_viewer_api.config import Settings


class TestSettings:
    def test_defaults(self):
        with patch.dict(os.environ, {}, clear=True):
            settings = Settings(_env_file=None)
            assert settings.ssh_host == ""
            assert settings.ssh_port == 22
            assert settings.log_level == "INFO"
            assert settings.remote_media_root == ""
            assert settings.ssh_key_directory == ""
            assert settings.ssh_key_name == ""
            assert settings.ssh_key_path == ""

    def test_from_env(self):
        env = {
            "LOG_LEVEL": "DEBUG",
            "SSH_HOST": "192.168.1.1",
            "SSH_USERNAME": "testuser",
            "SSH_PORT": "2222",
            "REMOTE_MEDIA_ROOT": "/srv/media",
        }
        with patch.dict(os.environ, env, clear=True):
            settings = Settings(_env_file=None)
            assert settings.log_level == "DEBUG"
            assert settings.ssh_host == "192.168.1.1"
            assert settings.ssh_username == "testuser"
            assert settings.ssh_port == 2222
            assert settings.remote_media_root == "/srv/media"

    def test_media_root_property(self):
        env = {"REMOTE_MEDIA_ROOT": "/mnt/data"}
        with patch.dict(os.environ, env, clear=True):
            settings = Settings(_env_file=None)
            assert settings.media_root == "/mnt/data"

    def test_path_prefix_property(self):
        env = {"REMOTE_PATH_PREFIX": "/srv"}
        with patch.dict(os.environ, env, clear=True):
            settings = Settings(_env_file=None)
            assert settings.path_prefix == "/srv"

    def test_ssh_key_path_joins_directory_and_name(self):
        env = {"SSH_KEY_DIRECTORY": "/root/.ssh", "SSH_KEY_NAME": "id_ed25519"}
        with patch.dict(os.environ, env, clear=True):
            settings = Settings(_env_file=None)
            assert settings.ssh_key_path == "/root/.ssh/id_ed25519"
