"""Google GenAI Content and Part types (TypedDict replicas).

This module contains TypedDict replicas of Google GenAI SDK content types.
These are used for type hints and validation in the LLM-Rosetta conversion layer.

Reference: google.genai.types (Content, Part, Blob, FileData, FunctionCall, etc.)
SDK Source: <python_env>/lib/python3.10/site-packages/google/genai/types.py
"""

from __future__ import annotations

from typing import Any, TypedDict, Union

__all__ = [
    # Blob / inline data
    "Blob",
    # File data
    "FileData",
    # Code execution
    "ExecutableCode",
    "CodeExecutionResult",
    # Function call / response
    "FunctionCall",
    "FunctionResponse",
    # Part types (convenience aliases)
    "TextPart",
    "InlineDataPart",
    "FileDataPart",
    "FunctionCallPart",
    "FunctionResponsePart",
    "ExecutableCodePart",
    "CodeExecutionResultPart",
    # Part union
    "Part",
    # Content
    "Content",
]


# ============================================================================
# Blob (inline binary data)
# ============================================================================


class Blob(TypedDict, total=False):
    """Content blob for inline binary data.

    Reference: google.genai.types.Blob
    """

    data: bytes | None
    """Required. Raw bytes."""

    display_name: str | None
    """Optional. Display name of the blob."""

    mime_type: str | None
    """Required. The IANA standard MIME type of the source data."""


# ============================================================================
# FileData (URI-based data)
# ============================================================================


class FileData(TypedDict, total=False):
    """URI-based data reference.

    Reference: google.genai.types.FileData
    """

    display_name: str | None
    """Optional. Display name of the file data."""

    file_uri: str | None
    """Required. URI."""

    mime_type: str | None
    """Required. The IANA standard MIME type of the source data."""


# ============================================================================
# Code Execution types
# ============================================================================


class ExecutableCode(TypedDict, total=False):
    """Code generated by the model that is meant to be executed.

    Reference: google.genai.types.ExecutableCode
    """

    code: str | None
    """Required. The code to be executed."""

    language: str | None
    """Required. Programming language of the code."""


class CodeExecutionResult(TypedDict, total=False):
    """Result of executing the ExecutableCode.

    Reference: google.genai.types.CodeExecutionResult
    """

    outcome: str | None
    """Required. Outcome of the code execution."""

    output: str | None
    """Optional. Contains stdout when code execution is successful,
    stderr or other description otherwise."""


# ============================================================================
# Function Call / Response types
# ============================================================================


class FunctionCall(TypedDict, total=False):
    """A function call generated by the model.

    Reference: google.genai.types.FunctionCall
    """

    id: str | None
    """The unique id of the function call."""

    args: dict[str, Any] | None
    """Optional. The function parameters and values in JSON object format."""

    name: str | None
    """Optional. The name of the function to call."""


class FunctionResponse(TypedDict, total=False):
    """A function response provided back to the model.

    Reference: google.genai.types.FunctionResponse
    """

    id: str | None
    """Optional. The id of the function call this response is for."""

    name: str | None
    """Required. The name of the function."""

    response: dict[str, Any] | None
    """Required. The function response in JSON object format."""


# ============================================================================
# Part types (convenience TypedDicts for specific part kinds)
# ============================================================================


class TextPart(TypedDict):
    """A Part containing only text content.

    Reference: google.genai.types.Part (text field)
    """

    text: str


class InlineDataPart(TypedDict):
    """A Part containing inline binary data.

    Reference: google.genai.types.Part (inline_data field)
    """

    inline_data: Blob


class FileDataPart(TypedDict):
    """A Part containing a file data URI reference.

    Reference: google.genai.types.Part (file_data field)
    """

    file_data: FileData


class FunctionCallPart(TypedDict):
    """A Part containing a function call.

    Reference: google.genai.types.Part (function_call field)
    """

    function_call: FunctionCall


class FunctionResponsePart(TypedDict):
    """A Part containing a function response.

    Reference: google.genai.types.Part (function_response field)
    """

    function_response: FunctionResponse


class ExecutableCodePart(TypedDict):
    """A Part containing executable code.

    Reference: google.genai.types.Part (executable_code field)
    """

    executable_code: ExecutableCode


class CodeExecutionResultPart(TypedDict):
    """A Part containing a code execution result.

    Reference: google.genai.types.Part (code_execution_result field)
    """

    code_execution_result: CodeExecutionResult


# ============================================================================
# Part (SDK-style unified TypedDict with all optional fields)
# ============================================================================


class Part(TypedDict, total=False):
    """A datatype containing media content.

    Exactly one field within a Part should be set, representing the specific
    type of content being conveyed.

    Reference: google.genai.types.Part
    """

    text: str | None
    """Optional. Text part (can be code)."""

    inline_data: Blob | None
    """Optional. Inlined bytes data."""

    file_data: FileData | None
    """Optional. URI based data."""

    function_call: FunctionCall | None
    """Optional. A predicted FunctionCall returned from the model."""

    function_response: FunctionResponse | None
    """Optional. The result output of a FunctionCall."""

    executable_code: ExecutableCode | None
    """Optional. Code generated by the model that is meant to be executed."""

    code_execution_result: CodeExecutionResult | None
    """Optional. Result of executing the ExecutableCode."""

    thought: bool | None
    """Optional. Indicates if the part is a thought/reasoning part."""

    thought_signature: str | None
    """Optional. Opaque signature for the thought."""


# ============================================================================
# Content
# ============================================================================


class Content(TypedDict, total=False):
    """Contains the multi-part content of a message.

    Reference: google.genai.types.Content
    """

    parts: list[Part] | None
    """List of parts that constitute a single message."""

    role: str | None
    """Optional. The producer of the content.
    Must be either 'user' or 'model'."""


# Convenience union type for all specific part kinds
PartUnion = Union[
    TextPart,
    InlineDataPart,
    FileDataPart,
    FunctionCallPart,
    FunctionResponsePart,
    ExecutableCodePart,
    CodeExecutionResultPart,
]
"""Union of all convenience Part TypedDicts."""
