"""
Settings viewer app to display current configuration.
"""

import gradio as gr

from apps.base_app import BaseApp


class SettingsViewerApp(BaseApp):
    """Display current application settings."""

    def get_settings_info(self) -> dict:
        """Get settings information as a dictionary."""
        if not self.settings:
            return {"error": "Settings not available"}

        return {
            "project_name": self.settings.project_name,
            "project_title": self.settings.project_title,
            "project_description": self.settings.project_description,
            "apps_directory": self.settings.apps_directory,
            "theme": self.settings.theme,
            "share": self.settings.share,
            "server_name": self.settings.server_name,
            "server_port": self.settings.server_port,
            "debug": self.settings.debug,
            "log_level": self.settings.log_level,
            "hf_token": "***" if self.settings.hf_token else "Not set",
            "openai_api_key": "***" if self.settings.openai_api_key else "Not set",
            "anthropic_api_key": "***"
            if self.settings.anthropic_api_key
            else "Not set",
            "hf_repo_dir": self.settings.hf_repo_dir or "Not set",
            "hf_cache_dir": self.settings.hf_cache_dir or "Not set",
            "repositories": [
                {"repo_name": repo.repo_name, "repo_url": repo.repo_url}
                for repo in self.settings.repositories
            ]
            if self.settings.repositories
            else "Not configured",
        }

    def create_interface(self) -> gr.Blocks:
        """Create the settings viewer interface."""
        with gr.Blocks() as interface:
            gr.Markdown("## Application Settings")
            gr.Markdown("Current configuration loaded from `config.yaml` and `.env`")

            with gr.Row():
                refresh_btn = gr.Button("🔄 Refresh Settings", variant="primary")

            settings_display = gr.JSON(label="Settings", value=self.get_settings_info())

            refresh_btn.click(
                fn=self.get_settings_info, inputs=None, outputs=settings_display
            )

            with gr.Accordion("Configuration Files", open=False):
                gr.Markdown("""
                ### Configuration Structure

                - **config.yaml**: General application settings (committed to git)
                - **.env**: Secrets and user-specific settings (git-ignored)
                - **.env.example**: Template for .env file

                ### Available Settings

                **From config.yaml:**
                - project_name, project_title, project_description
                - apps_directory, theme, share
                - server_name, server_port
                - repositories (optional): list of project repositories

                **From .env:**
                - API keys: hf_token, openai_api_key, anthropic_api_key
                - HF directories: hf_repo_dir, hf_cache_dir
                - User settings: debug, log_level

                Environment variables override config.yaml values.
                """)

        return interface


# Export the app class
app = SettingsViewerApp
