"""Command input widget.

Text input at the bottom of the screen for entering commands.
Posts CommandSubmitted message on Enter, clears input after submit.
"""

from __future__ import annotations

from textual.message import Message
from textual.widgets import Input


class CommandSubmitted(Message):
    """Message posted when a command is submitted.

    Attributes:
        command: The raw command text entered by the user.
    """

    def __init__(self, command: str) -> None:
        """Initialize the message.

        Args:
            command: The raw command text.
        """
        super().__init__()
        self.command = command


class CommandInput(Input):
    """Text input widget for entering commands.

    Extends Textual's Input widget with:
    - Film Noir placeholder text
    - CommandSubmitted message on Enter
    - Auto-clear after submit

    Note: Styling is defined in noir.tcss, not DEFAULT_CSS, to use theme variables.
    """

    def __init__(
        self,
        placeholder: str = "What's your next move, detective?",
        *,
        name: str | None = None,
        id: str | None = None,
        classes: str | None = None,
    ) -> None:
        """Initialize the command input.

        Args:
            placeholder: Placeholder text to display when empty.
            name: Widget name.
            id: Widget ID.
            classes: Space-separated CSS classes.
        """
        super().__init__(
            placeholder=placeholder,
            name=name,
            id=id,
            classes=classes,
        )

    def on_input_submitted(self, event: Input.Submitted) -> None:
        """Handle Enter key press.

        Posts CommandSubmitted message and clears the input.

        Args:
            event: The input submitted event.
        """
        # Get the command text
        command = self.value.strip()

        # Post the message (even if empty - let the handler decide)
        self.post_message(CommandSubmitted(command))

        # Clear the input
        self.value = ""

        # Prevent the default behavior
        event.stop()


__all__ = ["CommandInput", "CommandSubmitted"]
