from __future__ import annotations

import json

from .config import CHAMBER_VERSION, CONFIG_PATH, PACKAGE_ROOT, active_roots, registered_paths, runtime_storage_probe
from .evidence import gate_b_evidence_report


def main() -> None:
    required_package = [
        CONFIG_PATH,
        PACKAGE_ROOT / "web/static/index.html",
        PACKAGE_ROOT / "web/static/app.js",
        PACKAGE_ROOT / "web/static/struc_i_worker.js",
        PACKAGE_ROOT / "web/static/struc_i_v1_0_4_engine.js",
        PACKAGE_ROOT / "reference/struc_i_v1_0_4_galaxy_profiles.html",
    ]
    paths = registered_paths()
    required_registered = [
        paths["primary"], paths["ladder_manifest"], paths["ladders"], paths["profiles"], paths["r1_profiles"],
    ]
    missing = [str(path) for path in [*required_package, *required_registered] if not path.exists()]
    roots = active_roots()
    probe = runtime_storage_probe()
    gate_b = gate_b_evidence_report()
    report = {
        "chamber_version": CHAMBER_VERSION,
        "gate": "GATE_C",
        "package_root": str(PACKAGE_ROOT),
        "installed_in_project": bool(roots["installed_in_project"]),
        "project_root": str(roots["project_root"]),
        "run_root": str(roots["run_root"]),
        "export_root": str(roots["export_root"]),
        "missing_required_paths": missing,
        "gate_b_evidence_pass": bool(gate_b.get("gate_b_pass")),
        **probe,
        "preflight_pass": not missing and probe["runtime_write_test_pass"] and bool(gate_b.get("gate_b_pass")),
    }
    print(json.dumps(report, indent=2))
    if not report["preflight_pass"]:
        raise SystemExit(1)
    print("STARTUP_PREFLIGHT_PASS")


if __name__ == "__main__":
    main()
