"""
Theme Helper for Flask - Compatibility Layer

This module provides backward-compatible Flask integration for the theme system.
It wraps the ThemeRegistry from the themes module.

Usage:
    from local_deep_research.web.utils.theme_helper import theme_helper
    theme_helper.init_app(app)
"""

from flask import Flask
from loguru import logger

from ..themes import (
    get_theme_metadata,
    get_themes,
    get_themes_json,
    theme_registry,
)


class ThemeHelper:
    """Flask integration helper for the theme system.

    This class provides backward-compatible Flask integration,
    delegating to the ThemeRegistry for actual theme management.
    """

    def __init__(self, app: Flask | None = None):
        """Initialize the theme helper.

        Args:
            app: Optional Flask application instance
        """
        self.app = app
        if app:
            self.init_app(app)

    def init_app(self, app: Flask) -> None:
        """Initialize the helper with a Flask application.

        Registers Jinja2 template globals for theme access:
        - get_themes(): Returns list of theme IDs
        - get_themes_json(): Returns JSON array of theme IDs
        - get_theme_metadata(): Returns JSON object with theme metadata

        Args:
            app: Flask application instance
        """
        self.app = app

        # Register template functions
        app.jinja_env.globals["get_themes"] = get_themes
        app.jinja_env.globals["get_themes_json"] = get_themes_json
        app.jinja_env.globals["get_theme_metadata"] = get_theme_metadata

        theme_count = len(theme_registry.themes)
        logger.info(f"Theme system initialized with {theme_count} themes")

    def get_themes(self) -> list[str]:
        """Get list of available theme names.

        Returns:
            Sorted list of theme ID strings
        """
        return theme_registry.get_theme_ids()

    def clear_cache(self) -> None:
        """Clear the theme cache.

        Useful for development or when theme files are modified.
        """
        theme_registry.clear_cache()
        logger.debug("Theme cache cleared")


# Singleton instance for backward compatibility
theme_helper = ThemeHelper()
