"""
Alternative MODFLOW runner with completely hidden execution
This can be used if terminal flashing persists with the standard FloPy approach
"""

import subprocess
import os
import sys

def run_modflow_hidden(exe_path, model_name, model_ws):
    """
    Run MODFLOW with completely hidden terminal window
    """
    try:
        # Change to model directory
        old_dir = os.getcwd()
        os.chdir(model_ws)
        
        # Prepare command
        cmd = [exe_path, model_name + '.nam']
        
        # Create startup info for Windows to hide console
        if sys.platform.startswith('win'):
            startupinfo = subprocess.STARTUPINFO()
            startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
            startupinfo.wShowWindow = subprocess.SW_HIDE
        else:
            startupinfo = None
        
        # Run MODFLOW with hidden console
        result = subprocess.run(
            cmd,
            capture_output=True,
            text=True,
            startupinfo=startupinfo,
            creationflags=subprocess.CREATE_NO_WINDOW if sys.platform.startswith('win') else 0
        )
        
        # Restore directory
        os.chdir(old_dir)
        
        # Check if successful
        success = result.returncode == 0
        output = result.stdout + result.stderr
        
        return success, output
        
    except Exception as e:
        os.chdir(old_dir)
        return False, str(e)

# Example usage:
# success, output = run_modflow_hidden('mf2005.exe', 'model_name', '/path/to/model')