"""Git tools for AI agent integration."""

from typing import Optional
from langchain_core.tools import tool

from ieum.git.auto_commit import AutoCommitManager, git_status, git_diff


@tool
def git_status_tool() -> str:
    """Get the current git status showing modified, staged, and untracked files.

    Returns:
        Git status output showing the current state of the repository.
    """
    return git_status()


@tool
def git_diff_tool(staged: bool = False) -> str:
    """Get git diff showing changes in the repository.

    Args:
        staged: If True, show only staged changes. If False, show unstaged changes.

    Returns:
        Git diff output showing the changes.
    """
    return git_diff(staged=staged)


@tool
def generate_commit_message_tool(repo_path: str = ".") -> str:
    """Analyze git changes and generate an appropriate commit message.

    Args:
        repo_path: Path to the git repository (default: current directory).

    Returns:
        Generated commit message based on the changes.
    """
    manager = AutoCommitManager(repo_path)
    changes = manager.analyze_changes()

    if not changes.files:
        return "No changes to commit."

    message = manager.generate_commit_message(changes)
    return f"Suggested commit message:\n\n{message}\n\nChanges summary:\n- {len(changes.files)} files changed\n- Categories: {', '.join(changes.categories)}"


@tool
def git_commit_tool(message: str, repo_path: str = ".") -> str:
    """Create a git commit with the specified message.

    Args:
        message: The commit message.
        repo_path: Path to the git repository (default: current directory).

    Returns:
        Result of the commit operation.
    """
    manager = AutoCommitManager(repo_path)
    result = manager.commit(message)

    if result.success:
        return f"Commit successful!\nHash: {result.commit_hash}\nMessage: {result.message}"
    else:
        return f"Commit failed: {result.error}"
