#!/usr/bin/env python3
"""Minimal Windows-friendly shim for frequently used Makefile targets."""

from __future__ import annotations

import argparse
import subprocess
import sys
from pathlib import Path
from typing import List

ROOT = Path(__file__).resolve().parents[1]

COMMANDS = {
    "smoke-tests": [
        sys.executable,
        "-m",
        "pytest",
        "-q",
        "tests/examples/test_u6_sequential_demo.py",
        "tests/unit/operators/test_telemetry_warnings_extended.py",
        "tests/examples/test_atom_atlas_minimal.py",
        "tests/examples/test_periodic_table_basic.py",
    ],
    "clean": [
        sys.executable,
        "scripts/clean_generated_artifacts.py",
    ],
    "clean-scratch": [
        sys.executable,
        "-c",
        "import shutil; import os; shutil.rmtree(\"debug_scratch\", ignore_errors=True); print(\"Removed debug_scratch directory\")",],
    "report-tetrad": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Force_Fields_Tetrad_Exploration.ipynb",
    ],
    "report-atoms-molecules": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/TNFR_Atoms_And_Molecules_Study.ipynb",
    ],
    "report-phase-gated": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/TNFR_Phase_Gated_Coupling_Demo.ipynb",
    ],
    "atom-atlas-script": [
        sys.executable,
        "-c",
        "import os; os.makedirs('examples/output', exist_ok=True)",
        "&&",
        sys.executable,
        "examples/atom_atlas.py",
    ],
    "periodic-table-script": [
        sys.executable,
        "-c",
        "import os; os.makedirs('examples/output', exist_ok=True)",
        "&&",
        sys.executable,
        "examples/periodic_table_atlas.py",
    ],
    "triatomic-atlas-script": [
        sys.executable,
        "-c",
        "import os; os.makedirs('examples/output', exist_ok=True)",
        "&&",
        sys.executable,
        "examples/triatomic_atlas.py",
    ],
    "molecule-atlas-script": [
        sys.executable,
        "-c",
        "import os; os.makedirs('examples/output', exist_ok=True)",
        "&&",
        sys.executable,
        "examples/molecule_atlas.py",
    ],
    "phase-gated-script": [
        sys.executable,
        "-c",
        "import os; os.makedirs('examples/output', exist_ok=True)",
        "&&",
        sys.executable,
        "examples/phase_gated_coupling_demo.py",
    ],
    "elements-signature-script": [
        sys.executable,
        "-c",
        "import os; os.makedirs('examples/output', exist_ok=True)",
        "&&",
        sys.executable,
        "examples/elements_signature_study.py",
    ],
    "report-triatomic-atlas": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=1200",
        "--output-dir",
        "results/reports",
        "notebooks/TNFR_Triatomic_Atlas.ipynb",
    ],
    "report-molecule-atlas": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=1200",
        "--output-dir",
        "results/reports",
        "notebooks/TNFR_Molecule_Atlas.ipynb",
    ],
    "report-operator-completeness": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=1200",
        "--output-dir",
        "results/reports",
        "notebooks/Operator_Completeness_Search.ipynb",
    ],
    "report-operator-completeness-print": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "lab",
        "--HTMLExporter.theme=light",
        "--HTMLExporter.exclude_input=True",
        "--HTMLExporter.exclude_input_prompt=True",
        "--HTMLExporter.exclude_output_prompt=True",
        "--ExecutePreprocessor.timeout=1200",
        "--output-dir",
        "results/reports",
        "notebooks/Operator_Completeness_Search.ipynb",
    ],
    "report-interaction-sequences": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Interaction_Sequences.ipynb",
    ],
    "report-interaction-sequences-print": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "lab",
        "--HTMLExporter.theme=light",
        "--HTMLExporter.exclude_input=True",
        "--HTMLExporter.exclude_input_prompt=True",
        "--HTMLExporter.exclude_output_prompt=True",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Interaction_Sequences.ipynb",
    ],
    "report-emergent-particles": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Emergent_Particles_From_TNFR.ipynb",
    ],
    "report-fundamental-particles": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Fundamental_Particles_TNFR_Atlas.ipynb",
    ],
    "report-fundamental-particles-print": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "lab",
        "--HTMLExporter.theme=light",
        "--HTMLExporter.exclude_input=True",
        "--HTMLExporter.exclude_input_prompt=True",
        "--HTMLExporter.exclude_output_prompt=True",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Fundamental_Particles_TNFR_Atlas.ipynb",
    ],
    "report-particle-atlas-u6": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=1200",
        "--output-dir",
        "results/reports",
        "notebooks/TNFR_Particle_Atlas_U6_Sequential.ipynb",
    ],
    "report-periodic-table-classic": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=1500",
        "--output-dir",
        "results/reports",
        "notebooks/TNFR_Periodic_Table_Atlas.ipynb",
    ],
    "force-study-plots": [
        sys.executable,
        "benchmarks/plot_force_study_summaries.py",
    ],
    "report-all-classic": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Force_Fields_Tetrad_Exploration.ipynb",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Emergent_Particles_From_TNFR.ipynb",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Fundamental_Particles_TNFR_Atlas.ipynb",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "classic",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Interaction_Sequences.ipynb",
    ],
    "report-all-print": [
        sys.executable,
        "-c",
        "import os; os.makedirs('results/reports', exist_ok=True)",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "lab",
        "--HTMLExporter.theme=light",
        "--HTMLExporter.exclude_input=True",
        "--HTMLExporter.exclude_input_prompt=True",
        "--HTMLExporter.exclude_output_prompt=True",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Force_Fields_Tetrad_Exploration.ipynb",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "lab",
        "--HTMLExporter.theme=light",
        "--HTMLExporter.exclude_input=True",
        "--HTMLExporter.exclude_input_prompt=True",
        "--HTMLExporter.exclude_output_prompt=True",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Emergent_Particles_From_TNFR.ipynb",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "lab",
        "--HTMLExporter.theme=light",
        "--HTMLExporter.exclude_input=True",
        "--HTMLExporter.exclude_input_prompt=True",
        "--HTMLExporter.exclude_output_prompt=True",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Fundamental_Particles_TNFR_Atlas.ipynb",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "lab",
        "--HTMLExporter.theme=light",
        "--HTMLExporter.exclude_input=True",
        "--HTMLExporter.exclude_input_prompt=True",
        "--HTMLExporter.exclude_output_prompt=True",
        "--ExecutePreprocessor.timeout=900",
        "--output-dir",
        "results/reports",
        "notebooks/Interaction_Sequences.ipynb",
        "&&",
        sys.executable,
        "-m",
        "nbconvert",
        "--to",
        "html",
        "--execute",
        "--template",
        "lab",
        "--HTMLExporter.theme=light",
        "--HTMLExporter.exclude_input=True",
        "--HTMLExporter.exclude_input_prompt=True",
        "--HTMLExporter.exclude_output_prompt=True",
        "--ExecutePreprocessor.timeout=1200",
        "--output-dir",
        "results/reports",
        "notebooks/Operator_Completeness_Search.ipynb",
    ],
}


def run_command(cmd: List[str]) -> int:
    # Handle shell command chaining for mkdir && nbconvert patterns
    if "&&" in cmd:
        # Split on && and run sequentially
        parts = []
        current_part = []
        for item in cmd:
            if item == "&&":
                if current_part:
                    parts.append(current_part)
                    current_part = []
            else:
                current_part.append(item)
        if current_part:
            parts.append(current_part)
        
        for part in parts:
            proc = subprocess.run(part, cwd=ROOT, shell=True)
            if proc.returncode != 0:
                return proc.returncode
        return 0
    else:
        proc = subprocess.run(cmd, cwd=ROOT)
        return proc.returncode


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("target", nargs="?", help="Target name (e.g., smoke-tests, clean)")
    args, extras = parser.parse_known_args()

    if not args.target:
        print("Usage: make <target>")
        print("Available shim targets:")
        for key in COMMANDS:
            print(f"  - {key}")
        sys.exit(1)

    if args.target not in COMMANDS:
        print(
            f"Target '{args.target}' is not implemented in the Windows shim. "
            "Install GNU Make (or use WSL) for the full Makefile."
        )
        sys.exit(1)

    command = COMMANDS[args.target] + extras
    raise SystemExit(run_command(command))


if __name__ == "__main__":
    main()