#!/usr/bin/env python3
"""
Evaluate whether a contract-tuned Nemotron smoke may expand to full replay.
"""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path
from typing import Any


ROOT = Path(__file__).resolve().parents[2]
API_SRC = ROOT / "apps" / "api"
sys.path.insert(0, str(API_SRC))

from src.services.agent_nemotron_smoke_gate import (  # noqa: E402
    evaluate_nemotron_contract_tuned_smoke_gate,
)


def main() -> int:
    parser = argparse.ArgumentParser(
        description="Evaluate Nemotron contract-tuned smoke gate."
    )
    parser.add_argument("--runner-report", required=True, help="external runner report JSON")
    parser.add_argument("--output", required=True, help="smoke gate report JSON")
    parser.add_argument("--minimum-records", type=int, default=5)
    parser.add_argument("--latency-budget-ms", type=float, default=45_000.0)
    args = parser.parse_args()

    report = evaluate_nemotron_contract_tuned_smoke_gate(
        runner_report=_read_json(Path(args.runner_report)),
        source_reports={"runner_report": args.runner_report},
        minimum_records=args.minimum_records,
        latency_budget_ms=args.latency_budget_ms,
    ).to_dict()
    Path(args.output).write_text(
        json.dumps(report, ensure_ascii=False, indent=2, sort_keys=True) + "\n",
        encoding="utf-8",
    )
    print(json.dumps(report, ensure_ascii=False, sort_keys=True))
    return 0 if report["approved_for_full_replay"] else 2


def _read_json(path: Path) -> dict[str, Any]:
    with path.open(encoding="utf-8") as handle:
        return json.load(handle)


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