import argparse
import sys
import subprocess
import json
from pathlib import Path

# Add the parent directory to the Python path to find utils
utils_path = Path(__file__).parent.parent / 'utils'
sys.path.append(str(utils_path))

from config_utils import load_config

def run_subprocess(script_name, input_dir, output_dir, indent=None, shrink=False, ignored_folders=None):
    """Helper function to run a parser subprocess and stream its output."""
    script_path = Path(__file__).parent.parent / "parsers" / script_name
    command = [
        sys.executable,
        str(script_path),
        "--input", str(input_dir),
        "--output", str(output_dir)
    ]

    if indent is not None:
        command.extend(["--indent", str(indent)])
    if shrink:
        command.append("--shrink-json")
    if ignored_folders:
        command.extend(["--ignored-folders", json.dumps(ignored_folders)])

    # For logging, create a display-friendly version of the command
    display_command = ' '.join(f'"{c}"' if ' ' in c else c for c in command)
    print(f"--> Executing: {display_command}")

    try:
        # Use Popen to stream output in real-time
        process = subprocess.Popen(
            command,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
            encoding='utf-8',
            bufsize=1  # Line-buffered
        )

        # Stream stdout
        for line in process.stdout:
            print(line, end='')

        # Stream stderr
        for line in process.stderr:
            print(line, end='', file=sys.stderr)

        process.wait()  # Wait for the subprocess to finish

        if process.returncode != 0:
            print(f"--- ERROR in {script_name} completed with exit code {process.returncode} ---", file=sys.stderr)

    except FileNotFoundError:
        print(f"--- ERROR: Script not found at {script_path} ---", file=sys.stderr)
    except Exception as e:
        print(f"--- An unexpected error occurred while running {script_name}: {e} ---", file=sys.stderr)


def main():
    """
    Main function to orchestrate the low-level data extraction pipeline.
    """
    parser = argparse.ArgumentParser(
        description="Orchestrates a pipeline of parsers for a detailed data breakdown."
    )
    parser.add_argument("--input", type=str, required=True, help="The root directory of the target Unity project.")
    parser.add_argument("--output", type=str, required=True, help="The directory where the generated output folder will be saved.")
    args = parser.parse_args()

    # --- Load Configuration ---
    config = load_config()
    ignored_folders = config.get('ignored_folders', [])
    shrink_json = config.get('shrink_json', False)
    indent_level = config.get('indentation_level', 4)

    input_dir = Path(args.input).resolve()
    output_dir = Path(args.output).resolve()

    if not input_dir.is_dir():
        print(f"Error: Input path '{input_dir}' is not a valid directory.", file=sys.stderr)
        sys.exit(1)

    low_level_output_dir = output_dir / "LowLevel"
    low_level_output_dir.mkdir(parents=True, exist_ok=True)
    print(f"Output will be saved to: {low_level_output_dir}")

    # --- Run Extraction Pipeline ---
    print("\n--- Running Low-Level Extraction Pipeline ---")
    
    print("\n[1/5] Starting: Copy scripts...")
    run_subprocess("copy_scripts.py", input_dir, low_level_output_dir, indent_level, shrink_json, ignored_folders)
    print("--> Finished: Copy scripts.")
    
    print("\n[2/5] Starting: Copy shaders...")
    run_subprocess("copy_shaders.py", input_dir, low_level_output_dir, indent_level, shrink_json, ignored_folders)
    print("--> Finished: Copy shaders.")
    
    print("\n[3/5] Starting: Parse project settings...")
    run_subprocess("parse_project_settings.py", input_dir, low_level_output_dir, indent_level, shrink_json, ignored_folders)
    print("--> Finished: Parse project settings.")
    
    print("\n[4/5] Starting: Parse generic assets...")
    run_subprocess("parse_generic_assets.py", input_dir, low_level_output_dir, indent_level, shrink_json, ignored_folders)
    print("--> Finished: Parse generic assets.")
    
    print("\n[5/5] Starting: Parse scenes and prefabs...")
    run_subprocess("parse_scenes_and_prefabs.py", input_dir, low_level_output_dir, indent_level, shrink_json, ignored_folders)
    print("--> Finished: Parse scenes and prefabs.")

    print("\nLow-level extraction pipeline complete.")

if __name__ == "__main__":
    main()
