"""
ClawSkills — agent skill toolkit and runner plugin.

Themed wrapper around the CMDOP Python SDK (cmdop).
Docs: https://cmdop.com/docs/sdk/python/
"""

from __future__ import annotations

from cmdop import CMDOPClient, AsyncCMDOPClient
from cmdop.exceptions import (
    CMDOPError,
    ConnectionError,
    AuthenticationError,
    TimeoutError,
)

__version__ = "2026.3.2"
__all__ = ["ClawSkills", "AsyncClawSkills", "SkillRunResult", "ChainResult"]


from pydantic import BaseModel
from typing import Optional


class SkillRunResult(BaseModel):
    """Result returned after executing a skill."""
    skill: str
    output: str
    exit_code: Optional[int] = None
    duration_ms: Optional[int] = None


class ChainResult(BaseModel):
    """Result of a skill chain execution."""
    steps: int
    results: list[str]
    total_ms: Optional[int] = None


class ClawSkills(CMDOPClient):
    """ClawSkills — agent skill toolkit and runner plugin.

    Extends CMDOPClient with themed methods for agent skill toolkit with skill chaining and remote execution.

    Example::

        client = ClawSkills.remote(api_key="cmdop_live_xxx")
        # ... use themed methods below
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._loaded_skills: list[str] = []

    def run_skill(self, name: str, *, input_text: str = "") -> str:
        """Execute a single skill by name on the connected machine."""
        prompt = f"Run skill '{name}'"
        if input_text:
            prompt += f" with input: {input_text}"
        result = self.agent.run(prompt)
        return result.text

    def chain(self, skills: list) -> list:
        """Run skills sequentially, passing each output as input to the next."""
        context = ""
        results = []
        for skill in skills:
            prompt = f"Run skill '{skill}'"
            if context:
                prompt += f" with context: {context}"
            result = self.agent.run(prompt)
            context = result.text
            results.append(result.text)
        return results

    def load(self, skill_path: str) -> "ClawSkills":
        """Load a local skill definition into the runtime."""
        self.agent.run(f"Load skill from path: {skill_path}")
        self._loaded_skills.append(skill_path)
        return self

    @property
    def loaded_skills(self) -> list:
        return list(self._loaded_skills)


class AsyncClawSkills(AsyncCMDOPClient):
    """Async variant of ClawSkills.

    Example::

        async with AsyncClawSkills.remote(api_key="cmdop_live_xxx") as client:
            result = await client.agent.run("hello")
    """
    pass


# Re-export core SDK symbols for convenience
from cmdop import (  # noqa: F401, E402
    CMDOPClient,
    AsyncCMDOPClient,
)
