# #region AssistantToolListEnvironments [C:2] [TYPE Module] [SEMANTICS assistant, tool, environments, list]
# @BRIEF Handler for the "list_environments" tool — show available Superset environments.
# @LAYER API
# @RELATION DEPENDS_ON -> [AssistantToolRegistry]

from __future__ import annotations

from typing import Any

from sqlalchemy.orm import Session

from src.core.config_manager import ConfigManager
from src.core.logger import belief_scope, logger
from src.core.task_manager import TaskManager
from src.schemas.auth import User

from ._schemas import AssistantAction
from ._tool_registry import assistant_tool


# #region handle_list_environments [C:2] [TYPE Function]
@assistant_tool(
    operation="list_environments",
    domain="environments",
    description="Show available Superset environments with their id, name, and stage (DEV/PREPROD/PROD)",
    risk_level="safe",
    requires_confirmation=False,
)
@belief_scope("list_environments")
async def handle_list_environments(
    intent: dict[str, Any],
    current_user: User,
    task_manager: TaskManager,
    config_manager: ConfigManager,
    db: Session,
) -> tuple[str, str | None, list[AssistantAction]]:
    """Show available Superset environments."""
    envs = config_manager.get_environments()

    if not envs:
        return ("Нет доступных окружений.", None, [])

    lines = ["**Доступные окружения:**"]
    for env in envs:
        stage_icon = {"DEV": "🟢", "PREPROD": "🟡", "PROD": "🔴"}
        icon = stage_icon.get(env.stage, "⚪")
        prod_tag = " ⚠️ PRODUCTION" if env.is_production else ""
        default_tag = " ★ По умолчанию" if env.is_default else ""
        lines.append(
            f"{icon} **{env.id}** — {env.name}{prod_tag}{default_tag}"
        )

    actions = [
        AssistantAction(
            type="open_route",
            label="Открыть настройки окружений",
            target="/environments",
        )
    ]

    return ("\n".join(lines), None, actions)


# #endregion handle_list_environments
# #endregion AssistantToolListEnvironments
