"""
Tests that $HOME-style environment variables in research_library.storage_path
are correctly expanded via os.path.expandvars() in all code paths.
"""

import os
from pathlib import Path
from unittest.mock import Mock

import pytest


class TestDownloadServiceExpandvars:
    """DownloadService should expand env vars in storage_path."""

    def test_dollar_home_in_storage_path_is_expanded(self, mocker, tmp_path):
        """$HOME in storage_path should resolve to actual home directory."""
        real_home = str(tmp_path / "fakehome")
        os.makedirs(real_home, exist_ok=True)

        mock_settings = Mock()
        mock_settings.get_setting.return_value = "$HOME/mylib"
        mocker.patch(
            "local_deep_research.research_library.services.download_service.get_settings_manager",
            return_value=mock_settings,
        )
        mocker.patch("pathlib.Path.mkdir")
        mocker.patch(
            "local_deep_research.research_library.services.download_service.RetryManager"
        )
        mocker.patch.dict(os.environ, {"HOME": real_home})

        from local_deep_research.research_library.services.download_service import (
            DownloadService,
        )

        service = DownloadService(username="test_user")

        expected = str(Path(real_home) / "mylib")
        assert service.library_root == expected
        assert "$" not in service.library_root

    def test_custom_env_var_in_storage_path_is_expanded(self, mocker, tmp_path):
        """Custom env vars like $MY_DATA_DIR should be expanded."""
        data_dir = str(tmp_path / "data")
        os.makedirs(data_dir, exist_ok=True)

        mock_settings = Mock()
        mock_settings.get_setting.return_value = "$MY_DATA_DIR/library"
        mocker.patch(
            "local_deep_research.research_library.services.download_service.get_settings_manager",
            return_value=mock_settings,
        )
        mocker.patch("pathlib.Path.mkdir")
        mocker.patch(
            "local_deep_research.research_library.services.download_service.RetryManager"
        )
        mocker.patch.dict(os.environ, {"MY_DATA_DIR": data_dir})

        from local_deep_research.research_library.services.download_service import (
            DownloadService,
        )

        service = DownloadService(username="test_user")

        expected = str(Path(data_dir) / "library")
        assert service.library_root == expected
        assert "$" not in service.library_root


class TestExpandvarsUnit:
    """Unit tests verifying the expandvars pattern works correctly."""

    def test_expandvars_before_resolve(self, tmp_path):
        """Verify the expandvars + expanduser + resolve pattern works."""
        fake_home = str(tmp_path / "home")
        os.makedirs(fake_home, exist_ok=True)

        with pytest.MonkeyPatch.context() as mp:
            mp.setenv("HOME", fake_home)

            raw = "$HOME/research_library"
            result = Path(os.path.expandvars(raw)).expanduser().resolve()

            assert str(result) == str(Path(fake_home) / "research_library")
            assert "$" not in str(result)

    def test_tilde_and_envvar_both_expanded(self, tmp_path):
        """Both ~ and $VAR should be expanded in the same path."""
        custom_dir = str(tmp_path / "custom")
        os.makedirs(custom_dir, exist_ok=True)

        with pytest.MonkeyPatch.context() as mp:
            mp.setenv("MY_SUBDIR", "custom")

            raw = str(tmp_path / "$MY_SUBDIR/docs")
            result = Path(os.path.expandvars(raw)).expanduser().resolve()

            expected = str(Path(custom_dir) / "docs")
            assert str(result) == expected

    def test_no_envvar_path_unchanged(self):
        """Paths without env vars should pass through unchanged."""
        raw = "/absolute/path/to/library"
        result = str(Path(os.path.expandvars(raw)).expanduser().resolve())

        assert result == raw
