"""
Delete confirmation modal window for the hosts TUI application.

This module provides a confirmation dialog for deleting host entries.
"""

from textual.app import ComposeResult
from textual.containers import Vertical, Horizontal
from textual.widgets import Static, Button
from textual.screen import ModalScreen
from textual.binding import Binding

from ..core.models import HostEntry
from .styles import DELETE_CONFIRMATION_MODAL_CSS


class DeleteConfirmationModal(ModalScreen):
    """
    Modal screen for confirming entry deletion.

    Provides a confirmation dialog before deleting host entries.
    """

    CSS = DELETE_CONFIRMATION_MODAL_CSS

    BINDINGS = [
        Binding("escape", "cancel", "Cancel"),
        Binding("enter", "confirm", "Delete"),
    ]

    def __init__(self, entry: HostEntry):
        super().__init__()
        self.entry = entry

    def compose(self) -> ComposeResult:
        """Create the delete confirmation modal layout."""
        with Vertical(classes="delete-container"):
            yield Static("Delete Entry", classes="delete-title")

            yield Static(
                "Are you sure you want to delete this entry?", classes="delete-message"
            )

            # Show entry details
            hostnames_str = ", ".join(self.entry.hostnames)
            yield Static(
                f"{self.entry.ip_address} → {hostnames_str}", classes="entry-info"
            )

            if self.entry.comment:
                yield Static(f"Comment: {self.entry.comment}", classes="delete-message")

            with Horizontal(classes="button-row"):
                yield Button(
                    "Delete",
                    variant="error",
                    id="delete-button",
                    classes="default-button",
                )
                yield Button(
                    "Cancel (ESC)",
                    variant="default",
                    id="cancel-button",
                    classes="default-button",
                )

    def on_mount(self) -> None:
        """Focus cancel button by default for safety."""
        cancel_button = self.query_one("#cancel-button", Button)
        cancel_button.focus()

    def on_button_pressed(self, event: Button.Pressed) -> None:
        """Handle button presses."""
        if event.button.id == "delete-button":
            self.action_confirm()
        elif event.button.id == "cancel-button":
            self.action_cancel()

    def action_confirm(self) -> None:
        """Confirm deletion and close modal."""
        self.dismiss(True)

    def action_cancel(self) -> None:
        """Cancel deletion and close modal."""
        self.dismiss(False)
