"""Interactive quiz mode implementation."""

import sys

from rich.console import Console
from rich.prompt import Prompt

from quli_quiz.engine import QuizEngine
from quli_quiz.ui.display import display_question, display_results
from quli_quiz.ui.input import get_answer_interactive, get_answer_simple

console = Console()


def run_interactive_mode(engine: QuizEngine) -> None:
    """Run quiz in interactive mode (question-by-question)."""
    console.print("\n[bold green]Starting Interactive Quiz Mode[/bold green]\n")
    engine.start()

    try:
        while not engine.is_complete():
            question = engine.get_current_question()
            if question is None:
                break

            question_num = engine.current_question_index + 1
            total = len(engine.quiz.questions)

            display_question(question, question_num, total)

            try:
                answer = get_answer_interactive(question)
            except (OSError, ImportError):
                # Fallback if arrow keys don't work
                answer = get_answer_simple(question)
            except KeyboardInterrupt:
                console.print("\n[yellow]Quiz cancelled[/yellow]")
                sys.exit(0)

            user_answer = engine.submit_answer(answer)

            # Show immediate feedback
            if user_answer.is_correct:
                console.print("\n[bold green]✓ Correct![/bold green]")
            else:
                console.print("\n[bold red]✗ Incorrect[/bold red]")
                console.print(f"[yellow]Correct answer: {question.correct_answer}[/yellow]")

            if question.explanation:
                console.print(f"[dim]{question.explanation}[/dim]")

            if question_num < total:
                try:
                    Prompt.ask("\nPress Enter to continue", default="")
                except KeyboardInterrupt:
                    console.print("\n[yellow]Quiz cancelled[/yellow]")
                    sys.exit(0)

        # Show results
        result = engine.get_result()
        display_results(result)
    except KeyboardInterrupt:
        console.print("\n[yellow]Quiz cancelled[/yellow]")
        sys.exit(0)
