from __future__ import annotations
import argparse
import json
from pathlib import Path
from .config import load_config, resolve_registered_path
from .runner import run_analysis


def main():
    parser = argparse.ArgumentParser(description="Run GRAV-BRIDGE-I v0.1.0")
    parser.add_argument("--radial", type=Path)
    parser.add_argument("--signatures", type=Path)
    parser.add_argument("--config", type=Path)
    parser.add_argument("--output", type=Path, default=Path("outputs/manual_run"))
    parser.add_argument("--signature-permutations", type=int)
    parser.add_argument("--target-permutations", type=int)
    parser.add_argument("--bootstrap", type=int)
    args = parser.parse_args()

    config = load_config(args.config)
    radial = args.radial or resolve_registered_path(config, "radial")
    signatures = args.signatures or resolve_registered_path(config, "signatures")
    overrides = {"nulls": {}, "uncertainty": {}}
    if args.signature_permutations is not None:
        overrides["nulls"]["signature_permutations"] = args.signature_permutations
    if args.target_permutations is not None:
        overrides["nulls"]["target_curve_permutations"] = args.target_permutations
    if args.bootstrap is not None:
        overrides["uncertainty"]["galaxy_bootstrap_repetitions"] = args.bootstrap
    overrides = {k: v for k, v in overrides.items() if v}

    def progress(fraction, message):
        print(f"[{fraction:6.1%}] {message}", flush=True)

    result = run_analysis(radial, signatures, args.config, args.output, overrides, progress)
    print(json.dumps({"verdict": result["verdict"], "output": str(args.output)}, indent=2))


if __name__ == "__main__":
    main()
