from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any, Optional, Union

from ..time_utils import now_iso


@dataclass(frozen=True)
class Started:
    timestamp: str
    session_id: str
    thread_id: Optional[str] = None
    turn_id: Optional[str] = None


@dataclass(frozen=True)
class OutputDelta:
    timestamp: str
    content: str
    delta_type: str = "text"


@dataclass(frozen=True)
class ToolCall:
    timestamp: str
    tool_name: str
    tool_input: dict[str, Any]


@dataclass(frozen=True)
class ApprovalRequested:
    timestamp: str
    request_id: str
    description: str
    context: dict[str, Any]


@dataclass(frozen=True)
class TokenUsage:
    timestamp: str
    usage: dict[str, Any]


@dataclass(frozen=True)
class RunNotice:
    timestamp: str
    kind: str
    message: str = ""
    data: dict[str, Any] = field(default_factory=dict)


@dataclass(frozen=True)
class Completed:
    timestamp: str
    final_message: str = ""


@dataclass(frozen=True)
class Failed:
    timestamp: str
    error_message: str


RunEvent = Union[
    Started,
    OutputDelta,
    ToolCall,
    ApprovalRequested,
    TokenUsage,
    RunNotice,
    Completed,
    Failed,
]


__all__ = [
    "RunEvent",
    "Started",
    "OutputDelta",
    "ToolCall",
    "ApprovalRequested",
    "TokenUsage",
    "RunNotice",
    "Completed",
    "Failed",
    "now_iso",
]
