"""
Git Handlers - команды для работы с Git
"""

import logging
from typing import Dict, List, Any
from .base_handler import BaseHandler
from .response_formatter import ResponseFormatter

class GitHandlers(BaseHandler):
    """Обработчик Git команд"""
    
    def register_tools(self):
        """Регистрация всех Git инструментов"""
        
        # Git Commit Analyze
        self.register_tool(
            "mcp_nmp_git_commit_analyze",
            "Analyze git commit",
            self.git_commit_analyze,
            {
                "type": "object",
                "properties": {
                    "commit": {"type": "string", "description": "Commit hash or diff"}
                },
                "required": ["commit"]
            }
        )
        
        # Git Pattern Extract
        self.register_tool(
            "mcp_nmp_git_pattern_extract",
            "Extract patterns from git history",
            self.git_pattern_extract,
            {
                "type": "object",
                "properties": {
                    "repository": {"type": "string", "description": "Repository path"}
                },
                "required": ["repository"]
            }
        )
        
        # Commit Wisdom
        self.register_tool(
            "mcp_nmp_commit_wisdom",
            "Wisdom from commits",
            self.commit_wisdom,
            {
                "type": "object",
                "properties": {
                    "commits": {"type": "string", "description": "Commit history"}
                },
                "required": ["commits"]
            }
        )
        
        # Branch Strategy
        self.register_tool(
            "mcp_nmp_branch_strategy",
            "Branch strategies",
            self.branch_strategy,
            {
                "type": "object",
                "properties": {
                    "strategy": {"type": "string", "description": "Branch strategy"}
                },
                "required": ["strategy"]
            }
        )
        
        logging.info("✅ Зарегистрированы Git handlers (4 команды)")
    
    # Реализация методов
    async def git_commit_analyze(self, commit: str) -> str:
        """Анализ git коммита"""
        try:
            data = {"commit": commit}
            response = self.make_request("api/git/commit/analyze", data)
            return ResponseFormatter.format_generic(response)
        except Exception as e:
            error_response = await self.handle_error(e, "git_commit_analyze")
            return ResponseFormatter.format_error(error_response)
    
    async def git_pattern_extract(self, repository: str) -> str:
        """Извлечение паттернов из истории git"""
        try:
            data = {"repository": repository}
            response = self.make_request("api/git/pattern/extract", data)
            return ResponseFormatter.format_generic(response)
        except Exception as e:
            error_response = await self.handle_error(e, "git_pattern_extract")
            return ResponseFormatter.format_error(error_response)
    
    async def commit_wisdom(self, commits: str) -> str:
        """Мудрость из коммитов"""
        try:
            data = {"commits": commits}
            response = self.make_request("api/git/commit/wisdom", data)
            return ResponseFormatter.format_generic(response)
        except Exception as e:
            error_response = await self.handle_error(e, "commit_wisdom")
            return ResponseFormatter.format_error(error_response)
    
    async def branch_strategy(self, strategy: str) -> str:
        """Стратегии ветвления"""
        try:
            data = {"strategy": strategy}
            response = self.make_request("api/git/branch/strategy", data)
            return ResponseFormatter.format_generic(response)
        except Exception as e:
            error_response = await self.handle_error(e, "branch_strategy")
            return ResponseFormatter.format_error(error_response) 