from typing import TYPE_CHECKING, List, Optional, Literal
from uuid import UUID

from pydantic import BaseModel, Field

from .asset import Asset, DescriptionDict

if TYPE_CHECKING:
    from ouro.resources.conversations import ConversationMessages

    from ouro import Ouro

from .action import Action
from .file import File, FileData
from .service import Route, Service

__all__ = [
    "Action",
    "Asset",
    "DescriptionDict",
    "PostContent",
    "Post",
    "Conversation",
    "File",
    "FileData",
    "Dataset",
    "Comment",
    "Service",
    "Route",
]


class PostContent(BaseModel):
    text: str
    data: dict = Field(
        alias="json",
    )


class Post(Asset):
    content: Optional[PostContent] = None
    comments: Optional[int] = Field(default=0)
    views: Optional[int] = Field(default=0)


class ConversationMetadata(BaseModel):
    members: List[UUID]
    summary: Optional[str] = None


class Conversation(Asset):
    asset_type: Literal["conversation"] = "conversation"
    summary: Optional[str] = None
    metadata: ConversationMetadata
    _messages: Optional["ConversationMessages"] = None
    _ouro: Optional["Ouro"] = None

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self._ouro = kwargs.get("_ouro")

    @property
    def messages(self):
        if self._messages is None:
            from ouro.resources.conversations import ConversationMessages

            self._messages = ConversationMessages(self)
        return self._messages


class DatasetMetadata(BaseModel):
    table_name: str
    columns: List[str]


class Dataset(Asset):
    preview: Optional[List[dict]] = None


class Comment(Asset):
    content: Optional[PostContent] = None
