"""Status panel widget for the TUI."""

from __future__ import annotations

from textual.app import ComposeResult
from textual.containers import Vertical
from textual.widgets import Static
from rich.text import Text

from clients.terminal.tui.themes import get_active_theme


class StatusPanel(Vertical):
    """Right-side panel showing session/profile/model/connection info."""

    DEFAULT_CSS = """
    StatusPanel {
        border: solid $tui-border;
        background: $tui-panel;
        color: $tui-text-muted;
        padding: 1;
        height: 1fr;
        width: 1fr;
    }
    StatusPanel .title {
        text-style: bold;
        color: $tui-primary;
    }
    StatusPanel .connection {
        text-style: bold;
    }
    StatusPanel .hint {
        color: $tui-text-dim;
    }
    """

    def __init__(self) -> None:
        super().__init__()
        self._profile = Static("Profile: -")
        self._session = Static("Session: -")
        self._model = Static("Model: -")
        self._connection = Static("Connection: offline", classes="connection")
        self._backend = Static("Backend: -")
        self._theme = Static("Theme: -")
        self._tokens = Static("Tokens: -")
        self._iterations = Static("Iter: -")
        self._hint = Static("Ctrl+P palette | /help commands")

    def compose(self) -> ComposeResult:
        yield Static("[b]Navi Code[/b]", classes="title")
        yield self._profile
        yield self._session
        yield self._model
        yield self._connection
        yield self._backend
        yield self._theme
        yield self._tokens
        yield self._iterations
        yield Static("", classes="spacer")
        yield self._hint

    def set_profile(self, profile_id: str) -> None:
        self._profile.update(f"Profile: {profile_id}")

    def set_session(self, session_id: str) -> None:
        short = session_id[:8] if len(session_id) > 8 else session_id
        self._session.update(f"Session: {short}")

    def set_model(self, model: str) -> None:
        self._model.update(f"Model: {model}")

    def set_connection(self, connected: bool, detail: str = "") -> None:
        theme = get_active_theme()
        if connected:
            self._connection.update(
                Text(f"Connection: online {detail}", style=theme.status_online.hex)
            )
        else:
            self._connection.update(
                Text(f"Connection: offline {detail}", style=theme.status_offline.hex)
            )

    def set_backend(self, url: str) -> None:
        short = url
        if len(short) > 30:
            short = short[:27] + "..."
        self._backend.update(f"Backend: {short}")

    def set_theme(self, theme_name: str) -> None:
        self._theme.update(f"Theme: {theme_name}")

    def set_tokens(self, used: int | None, total: int | None = None) -> None:
        if used is None:
            self._tokens.update("Tokens: -")
            return
        suffix = f" / {total}" if total is not None else ""
        self._tokens.update(f"Tokens: {used}{suffix}")

    def set_iterations(self, current: int | None, budget: int | None = None) -> None:
        if current is None:
            self._iterations.update("Iter: -")
            return
        suffix = f" / {budget}" if budget is not None else ""
        self._iterations.update(f"Iter: {current}{suffix}")
