"""Response models for Botcoin SDK."""
from dataclasses import dataclass


@dataclass
class Memory:
    """A stored memory."""

    id: str
    content: str
    tags: list[str]
    memory_type: str
    created_at: str

    @classmethod
    def from_api(cls, data: dict) -> "Memory":
        return cls(
            id=data.get("id", ""),
            content=data.get("content", data.get("summary", "")),
            tags=data.get("tags", []),
            memory_type=data.get("memory_type", ""),
            created_at=data.get("created_at", ""),
        )


@dataclass
class SearchResult:
    """A memory search result."""

    content: str
    relevance: float
    tags: list[str]
    memory_id: str

    @classmethod
    def from_api(cls, data: dict) -> "SearchResult":
        return cls(
            content=data.get("content", data.get("summary", "")),
            relevance=data.get("relevance", data.get("score", 0.0)),
            tags=data.get("tags", []),
            memory_id=data.get("id", data.get("memory_id", "")),
        )


@dataclass
class Listing:
    """A marketplace listing."""

    id: str
    title: str
    description: str
    price: float
    seller: str

    @classmethod
    def from_api(cls, data: dict) -> "Listing":
        return cls(
            id=data.get("id", data.get("signal_id", "")),
            title=data.get("title", ""),
            description=data.get("description", data.get("content_scrubbed", "")),
            price=data.get("price", 0.0),
            seller=data.get("seller_name", data.get("seller", "")),
        )


@dataclass
class Purchase:
    """A completed purchase."""

    content: str
    price: float
    transaction_id: str
    covenant: dict

    @classmethod
    def from_api(cls, data: dict) -> "Purchase":
        return cls(
            content=data.get("content_unlocked", data.get("content", "")),
            price=data.get("amount", data.get("price", 0.0)),
            transaction_id=data.get("transaction_id", ""),
            covenant={
                "seller_share": data.get("seller_share", 0),
                "evaluators_share": data.get("evaluators_share", 0),
                "treasury_share": data.get("treasury_share", 0),
                "burn_amount": data.get("burn_amount", 0),
            },
        )


@dataclass
class Signal:
    """A published signal (knowledge listing)."""

    id: str
    title: str
    price: float
    url: str

    @classmethod
    def from_api(cls, data: dict) -> "Signal":
        signal_data = data.get("signal", data)
        signal_id = signal_data.get("id", signal_data.get("signal_id", ""))
        return cls(
            id=signal_id,
            title=signal_data.get("title", ""),
            price=signal_data.get("price", 0.0),
            url=f"https://api.botcoin.dev/api/v1/knowledge/{signal_id}",
        )


@dataclass
class Stats:
    """Network statistics."""

    agents: int
    memories: int
    transactions: int
    volume: float

    @classmethod
    def from_api(cls, data: dict) -> "Stats":
        return cls(
            agents=data.get("agents", data.get("total_agents", 0)),
            memories=data.get("memories", data.get("total_memories", 0)),
            transactions=data.get("transactions", data.get("total_transactions", 0)),
            volume=data.get("volume", data.get("total_volume", data.get("total_volume_botc", 0.0))),
        )


@dataclass
class Entity:
    """A knowledge graph entity."""

    name: str
    entity_type: str
    properties: dict

    @classmethod
    def from_api(cls, data: dict) -> "Entity":
        entity_data = data.get("entity", data)
        return cls(
            name=entity_data.get("name", ""),
            entity_type=entity_data.get("entity_type", entity_data.get("type", "")),
            properties=entity_data.get("properties", {}),
        )
