#!/usr/bin/env python3
# src/chuk_mcp_server/types/content.py
"""
Content - Content formatting with orjson optimization

This module provides content formatting functions using chuk_mcp types
with orjson optimization for maximum performance.
"""

from typing import Any

import orjson
from pydantic import BaseModel

from .base import AudioContent, EmbeddedResource, ImageContent, TextContent, content_to_dict, create_text_content


def format_content(content: Any) -> list[dict[str, Any]]:
    """Format content using chuk_mcp types with orjson optimization."""
    if isinstance(content, str):
        text_content = create_text_content(content)
        return [content_to_dict(text_content)]
    elif isinstance(content, dict):
        # 🚀 Use orjson for 2-3x faster JSON serialization
        json_str = orjson.dumps(content, option=orjson.OPT_INDENT_2).decode()
        text_content = create_text_content(json_str)
        return [content_to_dict(text_content)]
    elif isinstance(content, TextContent | ImageContent | AudioContent | EmbeddedResource):
        # Check MCP content types before generic BaseModel
        return [content_to_dict(content)]
    elif isinstance(content, BaseModel):
        # Handle other Pydantic models by converting to dict first
        json_str = orjson.dumps(content.model_dump(), option=orjson.OPT_INDENT_2).decode()
        text_content = create_text_content(json_str)
        return [content_to_dict(text_content)]
    elif isinstance(content, list):
        result = []
        for item in content:
            result.extend(format_content(item))
        return result
    else:
        text_content = create_text_content(str(content))
        return [content_to_dict(text_content)]


def format_content_as_text(content: Any) -> str:
    """Format any content as plain text."""
    if isinstance(content, str):
        return content
    elif isinstance(content, BaseModel):
        return orjson.dumps(content.model_dump(), option=orjson.OPT_INDENT_2).decode()
    elif isinstance(content, dict | list):
        return orjson.dumps(content, option=orjson.OPT_INDENT_2).decode()
    else:
        return str(content)


def format_content_as_json(content: Any) -> str:
    """Format any content as JSON string with orjson."""
    if isinstance(content, str):
        # Try to parse and re-format for consistency
        try:
            parsed = orjson.loads(content)
            return orjson.dumps(parsed, option=orjson.OPT_INDENT_2).decode()
        except orjson.JSONDecodeError:
            # If not valid JSON, wrap in quotes
            return orjson.dumps(content).decode()
    elif isinstance(content, BaseModel):
        return orjson.dumps(content.model_dump(), option=orjson.OPT_INDENT_2).decode()
    else:
        return orjson.dumps(content, option=orjson.OPT_INDENT_2).decode()


__all__ = ["format_content", "format_content_as_text", "format_content_as_json"]
