"""Compatibility stubs for execution-owned mutable state and results."""

from __future__ import annotations

from typing import Protocol, TypeAlias

from anima.core.schemas.enums import AgentState, LifecycleType
from anima.core.schemas.models import (
    AgentResult as _AgentResult,
    AgentStateData as _AgentStateData,
    LogEntry as _LogEntry,
    TokenUsage as _TokenUsage,
    ToolCallCount as _ToolCallCount,
)

AgentResult: TypeAlias = _AgentResult
AgentStateData: TypeAlias = _AgentStateData
LogEntry: TypeAlias = _LogEntry
TokenUsage: TypeAlias = _TokenUsage
ToolCallCount: TypeAlias = _ToolCallCount


class TokenUsageView(Protocol):
    """Narrow consumer view for token-budget accounting."""

    prompt_tokens: int
    completion_tokens: int
    total_tokens: int


class AgentResultView(Protocol):
    """Narrow consumer view for terminal execution outcomes."""

    final_state: AgentState
    result_ref: str | None
    token_usage: TokenUsageView | None
    error: str | None
    response_text: str | None


class PersistedExecutionStateView(Protocol):
    """Mutable persisted execution-state slice owned by `core/execution`."""

    instance_id: str
    trace_id: str
    persona_name: str
    state: AgentState
    lifecycle: LifecycleType
    parent_id: str | None
    children: list[str]
    depth: int
    max_depth: int
    pid: int | None
    mcp_socket: str | None
    created_at: str | None
    updated_at: str | None
    token_usage: TokenUsageView
    current_turn: int
    max_turns: int | None
    budget_total: int | None
    grace_budget: int
    error: str | None
    result_ref: str | None
    result: AgentResultView | None
    resume_after: str | None


class LifecycleLogEntryView(Protocol):
    """Narrow consumer view for execution event-log records."""

    ts: str
    event: str
    details: dict | None


__all__ = [
    "AgentResult",
    "AgentResultView",
    "AgentStateData",
    "LifecycleLogEntryView",
    "LogEntry",
    "PersistedExecutionStateView",
    "TokenUsage",
    "TokenUsageView",
    "ToolCallCount",
]
