"""
ClawHub — skill registry and agent marketplace 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.3"
__all__ = ["ClawHub", "AsyncClawHub", "SkillMeta", "InstallResult"]


from pydantic import BaseModel
from typing import Optional


class SkillMeta(BaseModel):
    """Metadata for a skill retrieved from the registry."""
    name: str
    version: str
    description: str
    author: Optional[str] = None
    downloads: int = 0


class InstallResult(BaseModel):
    """Result of installing a skill from the registry."""
    skill: str
    version: str
    installed: bool
    path: Optional[str] = None


class ClawHub(CMDOPClient):
    """ClawHub — skill registry and agent marketplace plugin.

    Extends CMDOPClient with themed methods for skill registry and agent marketplace with discovery and publishing.

    Example::

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

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

    def search(self, query: str, limit: int = 10) -> list:
        """Search skills in the registry by keyword."""
        result = self.agent.run(
            f"Search the skill registry for: {query}. Return top {limit} results."
        )
        return result.text

    def install(self, skill_name: str, version: str = "latest") -> dict:
        """Install a skill from the registry onto the connected machine."""
        result = self.agent.run(
            f"Install skill '{skill_name}@{version}' from the registry"
        )
        self._installed.append(skill_name)
        return {"skill": skill_name, "version": version, "installed": True}

    def publish(self, path: str, *, private: bool = False) -> dict:
        """Publish a local skill directory to the registry."""
        visibility = "private" if private else "public"
        result = self.agent.run(
            f"Publish skill from '{path}' as {visibility} to the registry"
        )
        return {"path": path, "published": True}

    @property
    def installed_skills(self) -> list:
        return list(self._installed)


class AsyncClawHub(AsyncCMDOPClient):
    """Async variant of ClawHub.

    Example::

        async with AsyncClawHub.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,
)
