"""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 info block: session/profile/model/connection.

    Auto-height — shrinks to its content so the TodoPanel below it fills the
    rest of the right column.
    """

    DEFAULT_CSS = """
    StatusPanel {
        border: solid $tui-border;
        background: $tui-panel;
        color: $tui-text-muted;
        padding: 1;
        height: auto;
        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._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._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}")
