"""Main menu screen."""

from __future__ import annotations

from textual.app import ComposeResult
from textual.binding import Binding
from textual.containers import Center, Vertical
from textual.screen import Screen
from textual.widgets import Footer, Header, OptionList, Static
from textual.widgets.option_list import Option


class MainMenuScreen(Screen):
    """Main application menu."""

    BINDINGS = [
        Binding("b", "build", "Build Template", show=True),
        Binding("a", "build_all", "Build All", show=True),
        Binding("d", "deploy", "Deploy VM", show=True),
        Binding("m", "manage", "Manage VMs", show=True),
        Binding("q", "quit", "Exit", show=True),
    ]

    def compose(self) -> ComposeResult:
        yield Header()
        with Center():
            with Vertical(id="main-menu"):
                yield Static(
                    "Proxmox VM Template Builder",
                    classes="wizard-title",
                )
                yield OptionList(
                    Option("Build New Template", id="build"),
                    Option("Build All Templates", id="build-all"),
                    Option("Deploy VM from Template", id="deploy"),
                    Option("Manage VMs and Templates", id="manage"),
                    Option("Exit", id="exit"),
                )
        yield Footer()

    def on_option_list_option_selected(
        self, event: OptionList.OptionSelected
    ) -> None:
        """Handle menu selection."""
        option_id = event.option.id

        if option_id == "build":
            self.action_build()
        elif option_id == "build-all":
            self.action_build_all()
        elif option_id == "deploy":
            self.action_deploy()
        elif option_id == "manage":
            self.action_manage()
        elif option_id == "exit":
            self.app.exit()

    def action_build(self) -> None:
        """Open template build wizard."""
        from tui.screens import TemplateBuildScreen

        self.app.push_screen(TemplateBuildScreen())

    def action_build_all(self) -> None:
        """Open build all templates screen."""
        from tui.widgets.dialogs import MessageDialog

        # TODO: Implement build all screen
        self.app.push_screen(
            MessageDialog(
                "Build All Templates",
                "This feature will build all distribution templates.\n\n"
                "Not yet implemented in Python TUI.\n"
                "Use ./build-template.sh for now.",
            )
        )

    def action_deploy(self) -> None:
        """Open VM deployment wizard."""
        from tui.screens import VMDeployScreen

        self.app.push_screen(VMDeployScreen())

    def action_manage(self) -> None:
        """Open VM management screen."""
        from tui.screens import VMManageScreen

        self.app.push_screen(VMManageScreen())

    def action_quit(self) -> None:
        """Exit the application."""
        self.app.exit()
