from typing import Annotated

from pydantic import Field
from pydantic_ai import RunContext
from suzent.core.agent_deps import AgentDeps
from suzent.tools.base import Tool, ToolErrorCode, ToolResult


class SkillTool(Tool):
    name = "SkillTool"
    tool_name = "skill_execute"
    deferrable = False
    session_guidance = (
        "When the task matches a known skill, use SkillTool early instead of "
        "reconstructing the workflow from scratch."
    )
    guidance_priority = 30

    def __init__(self):
        super().__init__()
        from suzent.skills import get_skill_manager

        self.skill_manager = get_skill_manager()

    def forward(
        self,
        ctx: RunContext[AgentDeps],
        skill_name: Annotated[
            str,
            Field(
                description="Name of the skill to load. Must match an available skill exactly."
            ),
        ],
    ) -> ToolResult:
        """Load a skill to gain specialized knowledge for a task.

        Use this tool IMMEDIATELY when the user's task matches a skill description,
        before attempting domain-specific work. The available skills are listed in
        the system prompt under 'Available Skills'.

        Args:
            skill_name: The name of the skill to load.
        """
        sm = ctx.deps.skill_manager
        if not sm:
            from suzent.skills import get_skill_manager

            sm = get_skill_manager()

        content = sm.get_skill_content(
            skill_name, sandbox_enabled=ctx.deps.sandbox_enabled
        )
        if content:
            return ToolResult.success_result(
                content,
                metadata={"skill_name": skill_name},
            )
        return ToolResult.error_result(
            ToolErrorCode.INVALID_ARGUMENT,
            f"Skill '{skill_name}' not found. Available skills: {sm.get_skill_descriptions()}",
            metadata={"skill_name": skill_name},
        )
