#!/usr/bin/env python3
"""
OrchestratorCore – führt EmotionEngine, MaturityEngine, BondEngine aus
Unionisiert Output in natiris_output.json
"""

import json
import subprocess
import os

CORE_DIR = os.path.expanduser("~/natiris/core")

def run(engine_name):
    cmd = ["python3", f"{CORE_DIR}/{engine_name}.py"]
    result = subprocess.run(cmd, capture_output=True, text=True)
    return result.returncode == 0, result.stdout, result.stderr

def combine_outputs():
    out = {}
    for name in ["EmotionEngine", "MaturityEngine", "BondEngine"]:
        base = name.split("Engine")[0]
        path = f"{CORE_DIR}/{base}_output.json"
        if os.path.exists(path):
            with open(path, "r") as f:
                out[base] = json.load(f)
        else:
            out[base] = None
    out_path = f"{CORE_DIR}/natiris_output.json"
    with open(out_path, "w") as f:
        json.dump(out, f, indent=2)
    return out

def main():
    print("Natiris Core – Phase 2: Engine-Orchestrierung")
    print("=" * 40)

    for name in ["EmotionEngine", "MaturityEngine", "BondEngine"]:
        ok, out, err = run(name)
        if ok:
            print(f"✅ {name}: OK")
        else:
            print(f"❌ {name}: FAILED\n{err}")

    combined = combine_outputs()
    print("\n— Combined Output —")
    print(json.dumps(combined, indent=2))

if __name__ == "__main__":
    main()
