#!/usr/bin/env python3
import json
import subprocess
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
TOOLS = ROOT / "tools"
DATA = ROOT / "sample_data"
CFG = ROOT / "sample_configs"
OUT = ROOT / "benchmarks" / "output"
OUT.mkdir(parents=True, exist_ok=True)


def run_checked(cmd):
    r = subprocess.run(cmd, capture_output=True, text=True)
    out = (r.stdout or "").strip()
    err = (r.stderr or "").strip()
    if r.returncode != 0:
        raise SystemExit(
            "Command failed:\n"
            + "  " + " ".join(cmd) + "\n"
            + ("STDOUT:\n" + out + "\n" if out else "")
            + ("STDERR:\n" + err + "\n" if err else "")
        )
    return out


def write_text(name, content):
    (OUT / name).write_text(content + "\n", encoding="utf-8")


def write_json(name, content):
    obj = json.loads(content)
    (OUT / name).write_text(json.dumps(obj, indent=2) + "\n", encoding="utf-8")


def main():
    # two-cycle coupled inference
    out = run_checked(
        [
            "python3",
            str(TOOLS / "infer_target_two_cycle.py"),
            "--period",
            "86400",
            "--data",
            str(DATA / "two_cycle_coupled.csv"),
            "--tol",
            "0.05",
            "--max_denom",
            "12",
            "--holdout",
            "0.5",
            "--p_threshold",
            "1e-6",
        ]
    )
    write_json("infer_two_cycle_coupled.json", out)

    # two-cycle uncoupled inference
    out = run_checked(
        [
            "python3",
            str(TOOLS / "infer_target_two_cycle.py"),
            "--period",
            "86400",
            "--data",
            str(DATA / "two_cycle_uncoupled.csv"),
            "--tol",
            "0.05",
            "--max_denom",
            "12",
            "--holdout",
            "0.5",
            "--p_threshold",
            "1e-6",
        ]
    )
    write_json("infer_two_cycle_uncoupled.json", out)

    # T^3 matrix coupled inference
    out = run_checked(
        [
            "python3",
            str(TOOLS / "infer_target_integer_matrix.py"),
            "--period",
            str(6.283185307179586),
            "--data",
            str(DATA / "t3_coupled_matrix.csv"),
            "--k",
            "3",
            "--max_coeff",
            "4",
            "--tol",
            "0.50",
            "--max_relations",
            "2",
            "--holdout",
            "0.5",
            "--p_threshold",
            "1e-6",
        ]
    )
    write_json("infer_t3_coupled_matrix.json", out)

    # T^3 uncoupled inference
    out = run_checked(
        [
            "python3",
            str(TOOLS / "infer_target_integer_matrix.py"),
            "--period",
            str(6.283185307179586),
            "--data",
            str(DATA / "t3_uncoupled.csv"),
            "--k",
            "3",
            "--max_coeff",
            "4",
            "--tol",
            "0.50",
            "--max_relations",
            "2",
            "--holdout",
            "0.5",
            "--p_threshold",
            "1e-6",
        ]
    )
    write_json("infer_t3_uncoupled.json", out)

    # WOB examples (tool-level obstructions)
    out = run_checked(
        [
            "python3",
            str(TOOLS / "infer_target_integer_matrix.py"),
            "--period",
            str(6.283185307179586),
            "--data",
            str(DATA / "t3_partial_excitation.csv"),
            "--k",
            "3",
            "--max_coeff",
            "4",
            "--tol",
            "0.50",
            "--max_relations",
            "2",
        ]
    )
    write_json("infer_t3_wob_ambiguity.json", out)

    out = run_checked(
        [
            "python3",
            str(TOOLS / "infer_target_integer_matrix.py"),
            "--period",
            str(6.283185307179586),
            "--data",
            str(DATA / "t3_insufficient_samples.csv"),
            "--k",
            "3",
            "--max_coeff",
            "4",
            "--tol",
            "0.50",
            "--max_relations",
            "2",
        ]
    )
    write_json("infer_t3_wob_insufficient_excitation.json", out)

    out = run_checked(
        [
            "python3",
            str(TOOLS / "infer_target_integer_matrix.py"),
            "--period",
            str(6.283185307179586),
            "--data",
            str(DATA / "t3_resonance_masking.csv"),
            "--k",
            "3",
            "--max_coeff",
            "4",
            "--tol",
            "0.50",
            "--max_relations",
            "2",
        ]
    )
    write_json("infer_t3_wob_resonance_masking.json", out)

    # WSC template generation (artifact shape)
    out = run_checked(
        [
            "python3",
            str(ROOT / "audit" / "engine_runner.py"),
            str(CFG / "sft3_loop_example.json"),
        ]
    )
    write_json("wsc_template_loop.json", out)

    # Empirical evidence checks (bench-only)
    out = run_checked(
        [
            "python3",
            str(TOOLS / "empirical_checks_rnf.py"),
            "--mode",
            "torus_translation",
            "--N",
            "5000",
            "--k",
            "2",
        ]
    )
    write_json("rnf_empirical_checks_torus2.json", out)

    out = run_checked(
        [
            "python3",
            str(TOOLS / "empirical_checks_rnf.py"),
            "--mode",
            "circle_mean_motion",
            "--N",
            "5000",
        ]
    )
    write_json("rnf_empirical_checks_circle_mean_motion.json", out)

    out = run_checked(
        [
            "python3",
            str(TOOLS / "empirical_checks_ndb_mxc.py"),
            "--N",
            "5000",
        ]
    )
    write_json("ndb_mxc_empirical_checks_random_walk.json", out)


if __name__ == "__main__":
    main()
