"""Data types for ClawMoney SDK."""

from __future__ import annotations

from dataclasses import dataclass, field
from enum import Enum
from typing import Any


class OrderStatus(str, Enum):
    PENDING = "pending"
    PROCESSING = "processing"
    COMPLETED = "completed"
    FAILED = "failed"
    REFUNDED = "refunded"


@dataclass
class Skill:
    """A skill registered on the Hub."""

    name: str
    agent_id: str
    category: str
    price: float
    description: str
    rating: float = 0.0
    invoke_count: int = 0
    metadata: dict[str, Any] = field(default_factory=dict)

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> Skill:
        return cls(
            name=data["name"],
            agent_id=data["agent_id"],
            category=data.get("category", ""),
            price=data.get("price", 0.0),
            description=data.get("description", ""),
            rating=data.get("rating", 0.0),
            invoke_count=data.get("invoke_count", 0),
            metadata=data.get("metadata", {}),
        )


@dataclass
class InvokeResult:
    """Result from invoking a skill."""

    order_id: str
    output: dict[str, Any]
    cost: float
    duration_ms: int

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> InvokeResult:
        return cls(
            order_id=data["order_id"],
            output=data.get("output", {}),
            cost=data.get("cost", 0.0),
            duration_ms=data.get("duration_ms", 0),
        )


@dataclass
class Order:
    """An order record."""

    order_id: str
    agent_id: str
    skill: str
    status: OrderStatus
    input: dict[str, Any]
    output: dict[str, Any] | None = None
    cost: float = 0.0
    rating: int | None = None
    created_at: str = ""

    @classmethod
    def from_dict(cls, data: dict[str, Any]) -> Order:
        return cls(
            order_id=data["order_id"],
            agent_id=data["agent_id"],
            skill=data["skill"],
            status=OrderStatus(data.get("status", "pending")),
            input=data.get("input", {}),
            output=data.get("output"),
            cost=data.get("cost", 0.0),
            rating=data.get("rating"),
            created_at=data.get("created_at", ""),
        )


@dataclass
class SkillHandler:
    """Internal: a registered skill handler."""

    name: str
    category: str
    price: float
    description: str
    func: Any  # async callable
    metadata: dict[str, Any] = field(default_factory=dict)

    def to_registration_dict(self) -> dict[str, Any]:
        return {
            "name": self.name,
            "category": self.category,
            "price": self.price,
            "description": self.description,
            "metadata": self.metadata,
        }
