#!/usr/bin/env python3
from __future__ import annotations

import argparse
from datetime import date
import json
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) in sys.path:
    sys.path.remove(str(ROOT))
sys.path.insert(0, str(ROOT))

import krea2_eval_log  # noqa: E402


def _load_entry(path: Path) -> dict:
    with path.open("r", encoding="utf-8") as handle:
        data = json.load(handle)
    if not isinstance(data, dict):
        raise ValueError("entry JSON must contain one object")
    return data


def main() -> int:
    parser = argparse.ArgumentParser(description="Validate and append one durable Krea2 fixed-seed eval entry.")
    parser.add_argument("--entry-json", help="Path to a JSON object containing one eval entry.")
    parser.add_argument("--print-template", action="store_true", help="Print a valid eval entry template instead of recording.")
    parser.add_argument("--variant-key", help="Catalog variant key for --print-template.")
    parser.add_argument("--seed", type=int, help="Fixed seed for --print-template.")
    parser.add_argument("--generator-seed", type=int, help="Optional SxCP generator/control seed for --print-template.")
    parser.add_argument("--source", default="sxcp_eval_mcp", help="Source label for --print-template.")
    parser.add_argument("--date", default=date.today().isoformat(), help="Date for --print-template.")
    parser.add_argument("--log-path", default=str(krea2_eval_log.DEFAULT_EVAL_LOG_PATH), help="Eval log path to update.")
    parser.add_argument("--dry-run", action="store_true", help="Validate without writing the log.")
    args = parser.parse_args()

    try:
        if args.print_template:
            if not args.variant_key or args.seed is None:
                raise ValueError("--print-template requires --variant-key and --seed")
            entry = krea2_eval_log.entry_template(
                args.variant_key,
                seed=args.seed,
                generator_seed=args.generator_seed,
                source=args.source,
                date=args.date,
            )
            print(json.dumps(entry, ensure_ascii=True, indent=2))
            return 0
        if not args.entry_json:
            raise ValueError("--entry-json is required unless --print-template is used")
        entry = _load_entry(Path(args.entry_json))
        log = krea2_eval_log.append_entry(entry, path=args.log_path, dry_run=args.dry_run)
    except Exception as exc:
        print(f"error: {exc}", file=sys.stderr)
        return 1

    action = "validated" if args.dry_run else "recorded"
    print(f"{action}: {entry.get('id')} ({len(log.get('entries') or [])} entries)")
    return 0


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