import json
import logging
from typing import Any

from pydantic import BaseModel, Field

logger = logging.getLogger()


def process_answer_to_string(answer: Any) -> str | None:
    """
    Convert any object input to a string representation.

    Args:
        answer: The answer object from agent.run() which could be any type

    Returns:
        String representation of the answer, or None if answer is None
    """
    if answer is None:
        return None

    if isinstance(answer, str):
        return answer

    try:
        if isinstance(answer, (list, dict)):
            try:
                return json.dumps(answer, ensure_ascii=False, indent=None)
            except (TypeError, ValueError):
                return str(answer)
        else:
            return str(answer)
    except Exception:
        logger.warning("Failed to convert answer to string")
    return ""


class AgentRunResult(BaseModel):
    """A structured response from an agent run."""

    model_id: str = Field(alias="model_id")
    """ID of the model used."""

    question: str = Field(alias="question")
    """The augmented question presented to the agent."""

    original_question: str = Field(alias="original_question")
    """The original question from the dataset."""

    answer: Any = Field(alias="answer")
    """The final answer provided by the agent."""

    reference_answer: Any = Field(alias="reference_answer")
    """The reference answer from the dataset."""

    intermediate_steps: list[dict[str, Any]] = Field(alias="intermediate_steps")
    """The intermediate steps taken by the agent."""

    start_time: float = Field(alias="start_time")
    """The start time of the agent run."""

    end_time: float = Field(alias="end_time")
    """The end time of the agent run."""

    token_counts: dict[str, int] | None = Field(alias="token_counts")
    """Token usage counts for the run."""

    class Config:
        populate_by_name = True
