"""Type definitions for the AAL SDK."""

from __future__ import annotations

from typing import Literal, NotRequired, TypedDict

ActionType = Literal["TOOL_CALL", "API_REQUEST", "CODE_EXECUTION", "MESSAGE", "CUSTOM"]
EventStatus = Literal["SUCCESS", "ERROR", "TIMEOUT", "PENDING"]


class AALClientConfig(TypedDict):
    """Configuration for the AAL client."""

    api_url: str
    api_key: str
    flush_interval: NotRequired[float]  # seconds, default 1.0
    batch_size: NotRequired[int]  # default 100


class LogActionInput(TypedDict):
    """Input for logging an action."""

    action_type: ActionType
    tool_name: NotRequired[str]
    parameters: NotRequired[dict[str, object]]
    response: NotRequired[dict[str, object]]
    status: EventStatus
    latency_ms: NotRequired[float]


class QueuedEvent(TypedDict):
    """Internal event with session/agent context."""

    session_id: str
    agent_id: str
    action_type: ActionType
    tool_name: NotRequired[str]
    parameters: NotRequired[dict[str, object]]
    response: NotRequired[dict[str, object]]
    status: EventStatus
    latency_ms: NotRequired[float]


class ActionEventResponse(TypedDict):
    """API response for an action event."""

    id: str
    tenant_id: str
    session_id: str
    agent_id: str
    action_type: str
    tool_name: str | None
    parameters: dict[str, object] | None
    response: dict[str, object] | None
    status: str
    latency_ms: float | None
    timestamp: str


class BatchResponse(TypedDict):
    """API response for batch event ingestion."""

    created: int
    events: list[ActionEventResponse]


class EventsQueryParams(TypedDict, total=False):
    """Query parameters for event listing."""

    session_id: str
    agent_id: str
    tool_name: str
    status: EventStatus
    start_time: str
    end_time: str
    limit: int
    cursor: str


class PaginatedEvents(TypedDict):
    """Paginated events response."""

    data: list[ActionEventResponse]
    cursor: str | None
    has_more: bool


class SessionResponse(TypedDict):
    """API response for a session."""

    id: str
    tenant_id: str
    agent_id: str
    status: str
    metadata: dict[str, object] | None
    started_at: str
    ended_at: str | None
    event_count: int
    events: list[ActionEventResponse]


class StartSessionConfig(TypedDict):
    """Configuration for starting a session."""

    agent_id: str
    session_id: NotRequired[str]
    metadata: NotRequired[dict[str, object]]
