"""Tests for web/utils/theme_helper.py."""

from unittest.mock import Mock, patch


class TestThemeHelperInit:
    """Tests for ThemeHelper initialization."""

    def test_init_without_app(self):
        """Test initialization without Flask app."""
        from local_deep_research.web.utils.theme_helper import ThemeHelper

        helper = ThemeHelper()
        assert helper.app is None

    def test_init_with_app_calls_init_app(self):
        """Test that initialization with app calls init_app."""
        from local_deep_research.web.utils.theme_helper import ThemeHelper

        mock_app = Mock()
        mock_app.jinja_env = Mock()
        mock_app.jinja_env.globals = {}

        with patch.object(ThemeHelper, "init_app") as mock_init:
            ThemeHelper(app=mock_app)
            mock_init.assert_called_once_with(mock_app)


class TestThemeHelperInitApp:
    """Tests for ThemeHelper.init_app method."""

    def test_init_app_sets_app_attribute(self):
        """Test that init_app sets the app attribute."""
        from local_deep_research.web.utils.theme_helper import ThemeHelper

        helper = ThemeHelper()
        mock_app = Mock()
        mock_app.jinja_env = Mock()
        mock_app.jinja_env.globals = {}

        helper.init_app(mock_app)
        assert helper.app is mock_app

    def test_init_app_registers_get_themes_global(self):
        """Test that get_themes is registered as Jinja global."""
        from local_deep_research.web.utils.theme_helper import ThemeHelper
        from local_deep_research.web.themes import get_themes

        helper = ThemeHelper()
        mock_app = Mock()
        mock_app.jinja_env = Mock()
        mock_app.jinja_env.globals = {}

        helper.init_app(mock_app)
        assert "get_themes" in mock_app.jinja_env.globals
        assert mock_app.jinja_env.globals["get_themes"] == get_themes

    def test_init_app_registers_get_themes_json_global(self):
        """Test that get_themes_json is registered as Jinja global."""
        from local_deep_research.web.utils.theme_helper import ThemeHelper
        from local_deep_research.web.themes import get_themes_json

        helper = ThemeHelper()
        mock_app = Mock()
        mock_app.jinja_env = Mock()
        mock_app.jinja_env.globals = {}

        helper.init_app(mock_app)
        assert "get_themes_json" in mock_app.jinja_env.globals
        assert mock_app.jinja_env.globals["get_themes_json"] == get_themes_json

    def test_init_app_registers_get_theme_metadata_global(self):
        """Test that get_theme_metadata is registered as Jinja global."""
        from local_deep_research.web.utils.theme_helper import ThemeHelper
        from local_deep_research.web.themes import get_theme_metadata

        helper = ThemeHelper()
        mock_app = Mock()
        mock_app.jinja_env = Mock()
        mock_app.jinja_env.globals = {}

        helper.init_app(mock_app)
        assert "get_theme_metadata" in mock_app.jinja_env.globals
        assert (
            mock_app.jinja_env.globals["get_theme_metadata"]
            == get_theme_metadata
        )


class TestThemeHelperGetThemes:
    """Tests for ThemeHelper.get_themes method."""

    def test_get_themes_returns_list(self):
        """Test that get_themes returns a list."""
        from local_deep_research.web.utils.theme_helper import ThemeHelper

        helper = ThemeHelper()
        themes = helper.get_themes()

        assert isinstance(themes, list)

    def test_get_themes_delegates_to_registry(self):
        """Test that get_themes delegates to theme_registry."""
        from local_deep_research.web.utils.theme_helper import ThemeHelper

        with patch(
            "local_deep_research.web.utils.theme_helper.theme_registry"
        ) as mock_registry:
            mock_registry.get_theme_ids.return_value = [
                "dark",
                "light",
                "ocean",
            ]

            helper = ThemeHelper()
            themes = helper.get_themes()

            mock_registry.get_theme_ids.assert_called_once()
            assert themes == ["dark", "light", "ocean"]


class TestThemeHelperClearCache:
    """Tests for ThemeHelper.clear_cache method."""

    def test_clear_cache_delegates_to_registry(self):
        """Test that clear_cache delegates to theme_registry."""
        from local_deep_research.web.utils.theme_helper import ThemeHelper

        with patch(
            "local_deep_research.web.utils.theme_helper.theme_registry"
        ) as mock_registry:
            helper = ThemeHelper()
            helper.clear_cache()

            mock_registry.clear_cache.assert_called_once()


class TestThemeHelperSingleton:
    """Tests for the singleton theme_helper instance."""

    def test_singleton_instance_exists(self):
        """Test that singleton theme_helper instance exists."""
        from local_deep_research.web.utils.theme_helper import theme_helper

        assert theme_helper is not None

    def test_singleton_is_theme_helper_instance(self):
        """Test that singleton is a ThemeHelper instance."""
        from local_deep_research.web.utils.theme_helper import (
            theme_helper,
            ThemeHelper,
        )

        assert isinstance(theme_helper, ThemeHelper)
