from __future__ import annotations

from typing import Protocol, runtime_checkable

from pydantic import BaseModel


class TextPart(BaseModel):
    text: str


class InlineBlobPart(BaseModel):
    encode_type: str = "base64"
    mime_type: str
    data: str


type Part = TextPart | InlineBlobPart


class Content(BaseModel):
    parts: list[Part]

    @property
    def text(self) -> str:
        content_text = ""
        for part in self.parts:
            if isinstance(part, TextPart):
                content_text += part.text
        return content_text

    @staticmethod
    def from_text(text: str) -> Content:
        return Content(parts=[TextPart(text=text)])


type ContentUnion = str | Content


@runtime_checkable
class Promptable(Protocol):
    def to_prompt(self) -> ContentUnion: ...
