#!/usr/bin/env python3
"""Add explicit MBBT/config provenance to the accepted Phase-6 replay client."""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path


PHASE6 = Path(__file__).resolve().parents[1] / "opprof-phase6"
sys.path.insert(0, str(PHASE6))

import opprof_phase6_client as base  # noqa: E402


def parser() -> argparse.ArgumentParser:
    result = argparse.ArgumentParser()
    result.add_argument("command", choices=("warmup", "run-anchor"))
    result.add_argument("--study", required=True)
    result.add_argument("--cell", required=True)
    result.add_argument("--anchor", type=float, required=True)
    result.add_argument("--tp", type=int, required=True)
    result.add_argument("--mns", type=int, required=True)
    result.add_argument("--mbbt", type=int, required=True)
    result.add_argument("--base-url", required=True)
    result.add_argument("--result-dir", required=True)
    result.add_argument("--disable-slo-early-stop", action="store_true")
    return result


def main() -> None:
    args = parser().parse_args()
    result = base.run_replay(args, warmup=args.command == "warmup")
    result.update(
        {
            "schema": "action-aware-pilot-result-v0",
            "config_id": args.cell,
            "mbbt": args.mbbt,
        }
    )
    base.atomic_json(Path(args.result_dir) / "result.json", result)
    print(
        json.dumps(
            {
                key: result[key]
                for key in (
                    "config_id",
                    "mns",
                    "mbbt",
                    "kind",
                    "pass_rate",
                    "feasible",
                )
            },
            sort_keys=True,
        )
    )


if __name__ == "__main__":
    main()
