"""SessionController — orchestration wrapper for SessionService."""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from ztlctl.controllers.base import BaseController

if TYPE_CHECKING:
    from ztlctl.services.result import ServiceResult


class SessionController(BaseController):
    """Thin wrapper over SessionService. All methods return ServiceResult."""

    def start(self, topic: str) -> ServiceResult:
        """Start a new session, returning the LOG-NNNN id."""
        from ztlctl.services.session import SessionService

        return SessionService(self._vault).start(topic)

    def close(self, *, summary: str | None = None) -> ServiceResult:
        """Close the active session with enrichment pipeline."""
        from ztlctl.services.session import SessionService

        return SessionService(self._vault).close(summary=summary)

    def reopen(self, session_id: str) -> ServiceResult:
        """Reopen a previously closed session."""
        from ztlctl.services.session import SessionService

        return SessionService(self._vault).reopen(session_id)

    def status(self) -> ServiceResult:
        """Return the active session summary, if any."""
        from ztlctl.services.session import SessionService

        return SessionService(self._vault).status()

    def log_entry(
        self,
        message: str,
        *,
        pin: bool = False,
        cost: int = 0,
        detail: str | None = None,
        entry_type: str = "log_entry",
        subtype: str | None = None,
        references: list[str] | None = None,
        metadata: dict[str, Any] | None = None,
    ) -> ServiceResult:
        """Append a log entry to the active session."""
        from ztlctl.services.session import SessionService

        return SessionService(self._vault).log_entry(
            message,
            pin=pin,
            cost=cost,
            detail=detail,
            entry_type=entry_type,
            subtype=subtype,
            references=references,
            metadata=metadata,
        )

    def cost(self, *, report: int | None = None) -> ServiceResult:
        """Query or report accumulated token cost for the active session."""
        from ztlctl.services.session import SessionService

        return SessionService(self._vault).cost(report=report)

    def context(
        self,
        *,
        topic: str | None = None,
        budget: int = 8000,
        ignore_checkpoints: bool = False,
    ) -> ServiceResult:
        """Build token-budgeted agent context payload."""
        from ztlctl.services.session import SessionService

        return SessionService(self._vault).context(
            topic=topic,
            budget=budget,
            ignore_checkpoints=ignore_checkpoints,
        )

    def brief(self) -> ServiceResult:
        """Quick orientation (delegates to ContextAssembler)."""
        from ztlctl.services.session import SessionService

        return SessionService(self._vault).brief()

    def extract_decision(self, session_id: str, *, title: str | None = None) -> ServiceResult:
        """Extract a decision note from a session log's pinned/decision entries."""
        from ztlctl.services.session import SessionService

        return SessionService(self._vault).extract_decision(session_id, title=title)
