"""CLI to evaluate predictions against labeled training scenarios."""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

from sicss2026.evaluation import evaluate_files, evaluate_predictions, load_labels_csv, load_predictions
from sicss2026.kit_paths import default_train_labels


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(
        description="Evaluate shopper predictions against labeled scenarios (training self-check)."
    )
    parser.add_argument(
        "--predictions",
        "-p",
        type=Path,
        required=True,
        help="Predictions file (.jsonl, .json, or .csv)",
    )
    parser.add_argument(
        "--labels",
        "-l",
        type=Path,
        default=default_train_labels(),
        help="Labeled CSV (default: public training split)",
    )
    parser.add_argument(
        "--require-all",
        action="store_true",
        help="Fail if any labeled record_id is missing from predictions",
    )
    parser.add_argument(
        "--json",
        action="store_true",
        help="Print machine-readable JSON only",
    )
    args = parser.parse_args(argv)

    if not args.predictions.exists():
        print(f"Predictions file not found: {args.predictions}", file=sys.stderr)
        return 1
    if not args.labels.exists():
        print(f"Labels file not found: {args.labels}", file=sys.stderr)
        return 1

    try:
        result = evaluate_files(
            args.predictions,
            args.labels,
            require_all=args.require_all,
        )
    except ValueError as exc:
        print(f"Evaluation failed: {exc}", file=sys.stderr)
        return 1

    if args.json:
        print(json.dumps(result.to_dict(), indent=2))
        return 0

    print(f"Records evaluated: {result.n_records}")
    print(f"MAE:  {result.mae:.4f}")
    print(f"RMSE: {result.rmse:.4f}")
    print(f"Brier: {result.brier:.4f}")
    print(f"Design-effect MAE: {result.design_effect_mae:.4f}")
    if result.persona_mae:
        print("\nPersona MAE:")
        for persona, mae in sorted(result.persona_mae.items()):
            print(f"  {persona}: {mae:.4f}")
    if result.missing_record_ids:
        print(f"\nMissing {len(result.missing_record_ids)} record_ids (not scored)")
    if result.extra_record_ids:
        print(f"Extra {len(result.extra_record_ids)} record_ids (ignored)")
    return 0


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