from janito.cli.console import shared_console
from janito.cli.chat_mode.shell.commands.base import ShellCmdHandler

class ToolsShellHandler(ShellCmdHandler):
    help_text = "List available tools"

    def run(self):
        try:

            import janito.tools  # Ensure all tools are registered
            from janito.tools.permissions import get_global_allowed_permissions
            registry = janito.tools.get_local_tools_adapter()
            tools = registry.list_tools()
            from rich.table import Table
            shared_console.print("Registered tools:")
            tool_instances = {t.tool_name: t for t in registry.get_tools()}
            if not tools:
                shared_console.print("No tools are enabled under the current permission settings.")
                return
            # Group tools by permission class
            perm_groups = {}
            def perm_class(perms):
                if perms.execute:
                    if perms.read and perms.write:
                        return "read-write-execute"
                    elif perms.read:
                        return "read-execute"
                    elif perms.write:
                        return "write-execute"
                    else:
                        return "execute-only"
                elif perms.read and perms.write:
                    return "read-write"
                elif perms.read:
                    return "read-only"
                elif perms.write:
                    return "write-only"
                else:
                    return "none"
            for tool in tools:
                inst = tool_instances.get(tool, None)
                perms = getattr(inst, 'permissions', None)
                if not perms:
                    group = "unknown"
                else:
                    group = perm_class(perms)
                perm_groups.setdefault(group, []).append(tool)
            # Build and print table
            table = Table(title="Tools by Permission Class")
            table.add_column("Permission Type", style="cyan", no_wrap=True)
            table.add_column("Tools", style="magenta")
            for group, tool_list in sorted(perm_groups.items()):
                table.add_row(group, " ".join(sorted(tool_list)))
            shared_console.print(table)

            
            # Find all possible execution tools (by permission: execute=True)
            exec_tools = []
            for tool_instance in registry.get_tools():
                perms = getattr(tool_instance, 'permissions', None)
                if perms and perms.execute:
                    exec_tools.append(tool_instance.tool_name)

            from janito.tools.permissions import get_global_allowed_permissions
            perms = get_global_allowed_permissions()
            if not perms.execute and exec_tools:
                shared_console.print("[yellow]⚠️  Warning: Execution tools (e.g., commands, code execution) are disabled. Use -x to enable them.[/yellow]")

        except Exception as e:
            shared_console.print(f"[red]Error loading tools: {e}[/red]")
