from enum import Enum
from typing import Any

from pydantic import BaseModel, ConfigDict

from a_simple_llm_kit.core._string_utils import simple_to_camel


class CamelModel(BaseModel):
    model_config = ConfigDict(
        alias_generator=simple_to_camel,
        populate_by_name=True,
    )


class ModelInfo(CamelModel):
    """Defines the structure for model information."""

    provider: str
    base_model: str
    model_name: str


class MediaType(Enum):
    TEXT = "text"
    IMAGE = "image"


class Usage(CamelModel):
    """A generic, provider-agnostic model for token usage."""

    prompt_tokens: int = 0
    completion_tokens: int = 0


class PipelineData(CamelModel):
    """Container for data passing through pipeline steps"""

    media_type: MediaType
    content: Any
    metadata: dict[str, Any] = {}


class TokenSummary(CamelModel):
    """Defines the structure for token usage in the API response."""

    prompt_tokens: int
    completion_tokens: int
    total_tokens: int
    cost_usd: float | None = None


class PerformanceSummary(CamelModel):
    """Defines the top-level structure for the performance block in the API response."""

    timing: dict[str, Any]
    trace_id: str
    tokens: TokenSummary | None = None


class ProgramMetadata(CamelModel):
    """Metadata for a DSPy program signature"""

    id: str
    name: str
    version: str
    code_hash: str
    description: str = ""
    tags: list[str] = []
    parent_id: str | None = None
    parent_version: str | None = None


class ProgramExecutionInfo(CamelModel):
    """Information about a specific program execution"""

    program_id: str
    program_version: str
    program_name: str
    model_id: str
    model_info: dict[str, Any] = {}
    execution_id: str
    timestamp: str
    trace_id: str | None = None


class ModelResponseInfo(CamelModel):
    """Defines the structure for the 'model' block in the response metadata."""

    id: str
    provider: str
    base_model: str
    model_name: str


class ProgramResponseInfo(CamelModel):
    """Defines the structure for the 'program' block in the response metadata."""

    id: str
    version: str
    name: str


class ResponseMetadata(CamelModel):
    """Defines the top-level 'metadata' object in the API response."""

    execution_id: str
    timestamp: str
    program: ProgramResponseInfo | None = None
    model: ModelResponseInfo | None = None
    performance: PerformanceSummary | None = None


class ImageProcessingMetadata(CamelModel):
    """Metadata generated by the ImageProcessor step."""

    processed: bool = True
    mime_type: str
    original_size: tuple[int, int]
    processed_size: tuple[int, int]
    compression_ratio: float
