"""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._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 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)
            )
