"""Input box widget for the TUI."""

from __future__ import annotations

from textual.app import ComposeResult
from textual.containers import Horizontal
from textual.widgets import Input, Static

from clients.terminal.tui.events import UserSubmitted


class InputBox(Horizontal):
    """Bottom prompt frame with input field."""

    DEFAULT_CSS = """
    InputBox {
        height: auto;
        min-height: 3;
        border: heavy $primary;
        padding: 0 1;
    }
    InputBox Input {
        height: auto;
        border: none;
        background: $surface;
    }
    """

    def __init__(self) -> None:
        super().__init__()
        self._prompt = Static("┃", classes="prompt-char")
        self._input = Input(placeholder="Ask anything...", classes="input-field")

    def compose(self) -> ComposeResult:
        yield self._prompt
        yield self._input

    def on_mount(self) -> None:
        self._input.focus()

    def on_input_submitted(self, event: Input.Submitted) -> None:
        text = event.value.strip()
        if text:
            self.post_message(UserSubmitted(text))
            self._input.value = ""

    def focus_input(self) -> None:
        self._input.focus()

    def set_prompt_char(self, char: str) -> None:
        self._prompt.update(char)
