#!/usr/bin/env python3
from __future__ import annotations

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

import yaml


ROOT = Path(__file__).resolve().parents[1]
REPORT = ROOT / "Temp" / "release_dag_run_v1.json"


def _cmd(*parts: str) -> list[str]:
    return [sys.executable, *parts] if parts and parts[0].endswith(".py") else list(parts)


def _release_commands() -> list[list[str]]:
    return [
        _cmd("tools/validate_specs.py"),
        _cmd("tools/validate_active_manifest.py", "--manifest", "runtime/active_artifact_manifest.yaml", "--strict"),
        _cmd("tools/validate_report_packet_sync_v1.py", "--packet", "Temp/final_decision_packet_active.json", "--report", "Temp/operational_report.json"),
        _cmd("tools/validate_report_section_completeness_v1.py"),
        _cmd("tools/validate_field_dictionary.py"),
        _cmd("tools/validate_number_provenance_strict_v3.py", "--ledger", "Temp/number_provenance_ledger_v4.json", "--report", "Temp/operational_report.md"),
        _cmd("tools/validate_low_capability_pack_v1.py", "--context", "Temp/final_context_for_llm_v4.yaml", "--contract", "spec/46_low_capability_execution_pack.yaml"),
        _cmd("tools/validate_golden_coverage_100.py"),
        _cmd("tools/validate_calibration_registry_v1.py"),
        _cmd("tools/validate_schema_model_generation_v1.py"),
        _cmd("tools/validate_gas_thin_adapter_v1.py"),
        _cmd("tools/validate_agents_shrink_v1.py"),
        _cmd("tools/validate_no_replay_live_mix_v1.py", "--json", "Temp/live_replay_separation_v2.json"),
        _cmd("tools/validate_renderer_no_calculation_v1.py"),
        _cmd("tools/validate_release_dag_v1.py", "--dag", "spec/41_release_dag.yaml", "--package", "package.json"),
    ]


def _full_commands() -> list[list[str]]:
    return [
        _cmd("tools/audit_repository_entropy_v1.py", "--root", ".", "--out", "runtime/baseline_manifest_v1.yaml"),
        *_release_commands(),
        ["dotnet", "run", "--project", str(ROOT / "src" / "dotnet" / "QuantEngine.Tools" / "QuantEngine.Tools.csproj"), "--", "packet-v4", "--packet=Temp/final_decision_packet_active.json", "--out=Temp/final_decision_packet_v4.json"],
        _cmd("tools/build_final_context_for_llm_v4.py", "--packet", "Temp/final_decision_packet_v4.json", "--out", "Temp/final_context_for_llm_v4.yaml"),
        _cmd("tools/build_number_provenance_ledger_v4.py", "--packet", "Temp/final_decision_packet_v4.json", "--out", "Temp/number_provenance_ledger_v4.json"),
        _cmd("tools/build_live_replay_separation_v2.py", "--hist", "Temp/proposal_evaluation_history.json", "--out", "Temp/live_replay_separation_v2.json"),
        _cmd("tools/build_bundle.py"),
        _cmd("tools/prepare_upload_zip.py"),
    ]


def _run(commands: list[list[str]], mode: str) -> list[dict[str, object]]:
    results: list[dict[str, object]] = []
    for cmd in commands:
        proc = subprocess.run(
            cmd,
            cwd=ROOT,
            capture_output=True,
            text=True,
            encoding="utf-8",
            errors="replace",
        )
        results.append(
            {
                "command": " ".join(cmd),
                "returncode": proc.returncode,
                "stdout": proc.stdout[-4000:],
                "stderr": proc.stderr[-4000:],
            }
        )
        if proc.returncode != 0:
            break
    REPORT.parent.mkdir(parents=True, exist_ok=True)
    REPORT.write_text(json.dumps({"formula_id": "RELEASE_DAG_RUN_V1", "mode": mode, "steps": results}, ensure_ascii=False, indent=2), encoding="utf-8")
    return results


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--mode", choices=("release", "full", "package"), default="release")
    args = ap.parse_args()

    dag_path = ROOT / "spec" / "41_release_dag.yaml"
    if not dag_path.exists():
        print("RELEASE_DAG_MISSING")
        return 1
    yaml.safe_load(dag_path.read_text(encoding="utf-8"))

    if args.mode == "package":
        commands = [_cmd("tools/prepare_upload_zip.py")]
    elif args.mode == "full":
        commands = _full_commands()
    else:
        commands = _release_commands()

    results = _run(commands, args.mode)
    ok = all(int(step.get("returncode") or 0) == 0 for step in results) and bool(results)
    print(json.dumps({"formula_id": "RELEASE_DAG_RUN_V1", "mode": args.mode, "step_count": len(results), "gate": "PASS" if ok else "FAIL"}, ensure_ascii=True, indent=2))
    return 0 if ok else 1


if __name__ == "__main__":
    raise SystemExit(main())
