#!/usr/bin/env python3
"""Report PixelRAG visual receipts as internal RAG candidate replay payloads."""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path


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

from services.pixelrag_rag_candidate_replay_service import (  # noqa: E402
    build_pixelrag_rag_candidate_replay_readback,
)


def main() -> int:
    parser = argparse.ArgumentParser(
        description="輸出 PixelRAG receipt 對內部 RAG candidate replay 的機器可讀讀回。"
    )
    parser.add_argument(
        "--artifact-root",
        help="PixelRAG visual evidence artifact root；預設使用 production/container 設定。",
    )
    parser.add_argument(
        "--platform-probe-worker-receipt-root",
        help="PixelRAG platform probe worker receipt root；預設使用 production/container 設定。",
    )
    parser.add_argument(
        "--platform",
        action="append",
        dest="platforms",
        help="限制平台，可重複指定，例如 --platform shopee_tw --platform coupang_tw。",
    )
    parser.add_argument(
        "--max-age-hours",
        type=int,
        default=168,
        help="receipt 最大新鮮度小時數。",
    )
    parser.add_argument(
        "--limit",
        type=int,
        default=50,
        help="最多輸出 receipt candidate 數。",
    )
    args = parser.parse_args()

    payload = build_pixelrag_rag_candidate_replay_readback(
        artifact_root=args.artifact_root,
        platform_probe_worker_receipt_root=args.platform_probe_worker_receipt_root,
        platform=tuple(args.platforms or ()),
        max_age_hours=args.max_age_hours,
        limit=args.limit,
    )
    print(json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True))
    return 0 if payload.get("success") else 1


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