# #region AssistantToolCommit [C:3] [TYPE Module] [SEMANTICS assistant, tool, git, commit]
# @BRIEF Handler for the "commit_changes" tool — commit dashboard repository changes.
# @LAYER API
# @RELATION DEPENDS_ON -> [AssistantToolRegistry]
# @RELATION DEPENDS_ON -> [GitService]

from __future__ import annotations

from typing import Any

from fastapi import HTTPException
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 ._resolvers import _resolve_dashboard_id_entity
from ._schemas import AssistantAction
from ._tool_registry import _check_any_permission, assistant_tool

from ._dispatch import _get_git_service


# #region handle_commit_changes [C:3] [TYPE Function]
@assistant_tool(
    operation="commit_changes",
    domain="git",
    description="Commit dashboard repository changes by dashboard id/slug/title",
    optional_entities=["dashboard_id", "dashboard_ref", "message"],
    risk_level="guarded",
    requires_confirmation=False,
    permission_checks=[("plugin:git", "EXECUTE")],
)
@belief_scope("commit_changes")
async def handle_commit_changes(
    intent: dict[str, Any],
    current_user: User,
    task_manager: TaskManager,
    config_manager: ConfigManager,
    db: Session,
) -> tuple[str, str | None, list[AssistantAction]]:
    """Commit dashboard repository changes."""
    _check_any_permission(current_user, [("plugin:git", "EXECUTE")])
    entities = intent.get("entities", {})
    dashboard_id = _resolve_dashboard_id_entity(entities, config_manager)
    commit_message = entities.get("message")
    if not dashboard_id:
        raise HTTPException(status_code=422, detail="Missing dashboard_id/dashboard_ref")
    _get_git_service().commit_changes(dashboard_id, commit_message, None)
    return ("Коммит выполнен успешно.", None, [])


# #endregion handle_commit_changes
# #endregion AssistantToolCommit
