"""Compatibility stubs for anima-owned conversation and compaction types."""

from __future__ import annotations

from datetime import datetime
from typing import Literal, Mapping, Protocol, TypeAlias, TypedDict

from anima.core.schemas.models import (
    CompactionConfig as _CompactionConfig,
    ContextBundle as _ContextBundle,
    RawTailEntry as _RawTailEntry,
    SummaryRef as _SummaryRef,
)

CompactionConfig: TypeAlias = _CompactionConfig
RawTailEntry: TypeAlias = _RawTailEntry
SummaryRef: TypeAlias = _SummaryRef
ContextBundle: TypeAlias = _ContextBundle
ConversationRole = Literal["user", "assistant", "tool"]
CompactionMessage: TypeAlias = Mapping[str, object]
SystemRequestMessage: TypeAlias = Mapping[str, object]
CompactionHistory: TypeAlias = list[CompactionMessage]


class SessionEvent(TypedDict, total=False):
    """Session/log event shape consumed by context-bundle extraction."""

    ts: str
    event: str
    details: dict[str, object]


class ConversationMessageView(Protocol):
    """Canonical anima message shape for prune and compaction flows."""

    role: ConversationRole
    speaker_id: str | None
    content: str


class ConversationSummaryView(Protocol):
    """Canonical summary reference shape for inherited conversation context."""

    summary_id: str
    model_id: str
    prompt_digest: str | None
    input_digest: str | None
    created_at: datetime


class ContextBundleView(Protocol):
    """Shared-context handoff shape consumed by child-launch flows."""

    bundle_id: str
    inherited_from: str | None
    raw_tail: list[ConversationMessageView] | None
    summary_refs: list[ConversationSummaryView] | None
    artifact_refs: list[str] | None


class CompactionSessionView(Protocol):
    """Read-only session view for prune and compaction decisions."""

    raw_tail: list[ConversationMessageView]
    summary_refs: list[ConversationSummaryView]
    artifact_refs: list[str]


__all__ = [
    "CompactionHistory",
    "CompactionConfig",
    "CompactionMessage",
    "CompactionSessionView",
    "ContextBundle",
    "ContextBundleView",
    "ConversationMessageView",
    "ConversationRole",
    "ConversationSummaryView",
    "RawTailEntry",
    "SessionEvent",
    "SystemRequestMessage",
    "SummaryRef",
]
