"""Typed event hierarchy for streaming execution responses."""

from __future__ import annotations

from typing import Literal, Union

from pydantic import BaseModel, ConfigDict

from baponi.types import SandboxResult


class StatusEvent(BaseModel):
    """Execution status change (e.g. queued → running)."""

    model_config = ConfigDict(frozen=True)

    type: Literal["status"]
    trace_id: str
    status: str
    seq: int


class OutputEvent(BaseModel):
    """Real-time stdout/stderr output chunk."""

    model_config = ConfigDict(frozen=True)

    type: Literal["output"]
    stream: str  # "stdout" or "stderr"
    data: str
    seq: int


class KeepaliveEvent(BaseModel):
    """Keepalive heartbeat from the server."""

    model_config = ConfigDict(frozen=True)

    type: Literal["keepalive"]
    seq: int


class ResultEvent(BaseModel):
    """Final execution result with full SandboxResult."""

    model_config = ConfigDict(frozen=True)

    type: Literal["result"]
    trace_id: str
    status: str
    result: SandboxResult
    output_truncated: bool = False
    seq: int


ExecutionEvent = Union[StatusEvent, OutputEvent, KeepaliveEvent, ResultEvent]
