"""Integration tests for interactive prompt functionality.

Simplified integration tests that validate prompt functionality works correctly
with the actual framework API. Due to framework lifecycle limitations (phases
complete after first run), these tests focus on the most critical workflows.

Note: More comprehensive unit testing exists in test_param_prompts.py (65 tests).
      Issue #63 tracks adding add_cycles() API for more flexible testing.
"""

import sys
from io import StringIO
from unittest.mock import patch
from subprocess import run, PIPE

import pytest

from spafw37 import core as spafw37
from spafw37 import param
from spafw37 import cycle
from spafw37.constants.param import (
    PARAM_NAME,
    PARAM_TYPE,
    PARAM_TYPE_TEXT,
    PARAM_PROMPT,
    PARAM_PROMPT_TIMING,
    PARAM_PROMPT_REPEAT,
    PARAM_PROMPT_HANDLER,
    PARAM_INPUT_FILTER,
    PARAM_REQUIRED,
    PROMPT_ON_START,
    PROMPT_ON_COMMAND,
    PROMPT_ON_COMMANDS,
    PROMPT_REPEAT_NEVER,
)
from spafw37.constants.command import (
    COMMAND_NAME,
    COMMAND_ACTION,
    COMMAND_NEXT_COMMANDS,
    COMMAND_PROMPT_PARAMS,
)


@pytest.fixture(autouse=True)
def reset_module_state():
    """Reset all framework module state before and after each test to ensure isolation."""
    from spafw37 import command, config
    from spafw37.constants.phase import PHASE_DEFAULT
    
    # Reset param module
    param._prompted_params.clear()
    param._global_prompt_handler = None
    param._output_handler = None
    param._max_prompt_retries = 3
    param._params = {}
    
    # Reset command module
    command._commands = {}
    command._command_queue = []
    command._phases_completed = []
    command._current_phase = None
    command._phase_order = [PHASE_DEFAULT]
    command._phases = {PHASE_DEFAULT: []}
    
    # Reset config module
    config._config = {}
    
    # Reset cycle module
    cycle.reset_cycle_state()
    
    yield
    
    # Reset again after test
    param._prompted_params.clear()
    param._global_prompt_handler = None
    param._output_handler = None
    param._max_prompt_retries = 3
    param._params = {}
    
    command._commands = {}
    command._command_queue = []
    command._phases_completed = []
    command._current_phase = None
    command._phase_order = [PHASE_DEFAULT]
    command._phases = {PHASE_DEFAULT: []}
    
    config._config = {}
    
    cycle.reset_cycle_state()


def test_prompt_on_start_integration(monkeypatch):
    """Integration test: PROMPT_ON_START params work end-to-end.
    
    This test validates the complete workflow from CLI invocation through
    prompt execution to command completion, verifying that:
    - Params with PROMPT_ON_START timing are prompted after CLI parsing
    - Prompted values are available to commands
    - The integration between cli.py and param.py works correctly
    
    Note: Framework is designed to run phases once per process. If phases have
          already completed in previous tests, this test will be skipped.
    """
    command_received_value = None
    
    def test_action():
        nonlocal command_received_value
        command_received_value = param.get_param(param_name='start_param_integration')
    
    spafw37.add_params([{
        PARAM_NAME: 'start_param_integration',
        PARAM_TYPE: PARAM_TYPE_TEXT,
        PARAM_PROMPT: 'Enter value',
        PARAM_PROMPT_TIMING: PROMPT_ON_START,
    }])
    
    spafw37.add_commands([{
        COMMAND_NAME: 'test_cmd_integration',
        COMMAND_ACTION: test_action,
    }])
    
    monkeypatch.setattr('builtins.input', lambda prompt: 'integration_test_value')
    
    try:
        spafw37.run_cli(['test_cmd_integration'])
    except SystemExit as e:
        if e.code == 1:
            # Check if it was due to completed phases
            pytest.skip("Framework phases already completed (designed to run once per process)")
        raise
    
    assert command_received_value == 'integration_test_value'
