#    Copyright (C) 2025 James Brotosky, Brandon Wickline
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published
#    by the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <https://www.gnu.org/licenses/>.

from typing import List, Optional

from textual.app import ComposeResult
from textual.binding import Binding
from textual.screen import Screen

from models.agent import Agent
from TUI.Widgets.OTP_generate import OTPGenerator


class OTPWorkflowScreen(Screen):
    """Screen that handles the OTP generation workflow without agent selection."""

    BINDINGS = [
        Binding("escape", "go_back", "Back"),
        Binding("q", "main_menu", "Main Menu"),
    ]

    def __init__(self, selected_agents: Optional[List[Agent]]):
        super().__init__()
        self.selected_agents = selected_agents

    def compose(self) -> ComposeResult:
        """Directly show the OTP generator for the selected agents."""
        yield OTPGenerator(self.selected_agents)

    def action_go_back(self) -> None:
        """Handle escape key to go back one screen."""
        self.app.pop_screen()

    def action_main_menu(self) -> None:
        """Handle q key to go back to main menu."""
        while len(self.app.screen_stack) > 2:
            self.app.pop_screen()

    def on_otp_generator_otp_info(self, message: OTPGenerator.OTPInfo) -> None:
        """Handle OTP generation request - pass it up to the app level if needed."""
