from __future__ import annotations

from datetime import datetime, timezone
from typing import Any, Literal

from pydantic import BaseModel, Field


AgentEventType = Literal[
    "agent.execution.decided",
    "agent.parallel.assessed",
    "agent.skill.shortlisted",
    "agent.task_graph.built",
    "agent.subtask.started",
    "agent.subtask.completed",
    "agent.merge.completed",
    "agent.tool.start",
    "agent.tool.result",
    "agent.verify.started",
    "agent.verify.completed",
    "agent.retrospective.created",
    "agent.learning.decision",
    "agent.skill.lifecycle.changed",
    "agent.rollback.triggered",
    "agent.created",
    "agent.spawn.blocked",
    "agent.message.sent",
    "agent.message.received",
    "agent.interrupt.requested",
    "agent.interrupt.completed",
    "agent.recovery.started",
    "agent.recovery.completed",
    "agent.task.interrupted",
    "agent.task.recovered",
    "agent.task.reassigned",
    "agent.collaboration.budget.updated",
    "agent.isolation.selected",
    "agent.isolation.fallback",
    "agent.cost.updated",
    "agent.cost.warning",
    "agent.phase.changed",
    "agent.checkpoint.recorded",
    "agent.error",
]
AgentEventSeverity = Literal["info", "warning", "error"]


class AgentEvent(BaseModel):
    event_id: str
    event_type: AgentEventType
    timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
    conversation_id: str | None = None
    agent_id: str | None = None
    sub_commander_id: str | None = None
    task_id: str | None = None
    parent_task_id: str | None = None
    child_task_id: str | None = None
    thread_id: str | None = None
    message_id: str | None = None
    interrupt_id: str | None = None
    recovery_id: str | None = None
    payload: dict[str, Any] = Field(default_factory=dict)
    severity: AgentEventSeverity = "info"
