from __future__ import annotations

from typing import TYPE_CHECKING, ClassVar

from textual.containers import Horizontal, Vertical
from textual.screen import ModalScreen
from textual.widgets import Button, Label

if TYPE_CHECKING:
    from textual.app import ComposeResult
    from textual.binding import BindingType


class ConfirmDeleteModal(ModalScreen[bool]):
    BINDINGS: ClassVar[list[BindingType]] = [
        ("left", "focus_previous", "Previous"),
        ("right", "focus_next", "Next"),
        ("escape", "cancel", "Cancel"),
    ]

    def __init__(self, target_name: str) -> None:
        self._target_name = target_name
        super().__init__()

    def compose(self) -> ComposeResult:
        with Vertical(id="modal-dialog"):
            yield Label("[#ef4444]Confirm Delete[/]", id="modal-title")
            yield Label(
                "Are you sure you want to delete"
                f' [bold #ef4444]"{self._target_name}"[/]?\n'
                "[#9ca3af]This action cannot be undone.[/]",
                id="modal-message",
            )
            with Horizontal(id="modal-buttons"):
                yield Button("Cancel", id="modal-cancel", variant="primary")
                yield Button("Delete", id="modal-confirm", variant="error")

    def on_mount(self) -> None:
        self.query_one("#modal-cancel", Button).focus()

    def on_button_pressed(self, event: Button.Pressed) -> None:
        self.dismiss(event.button.id == "modal-confirm")

    def action_cancel(self) -> None:
        self.dismiss(False)
