#!/usr/bin/env python3
"""Run or dry-run the PixelRAG platform probe worker."""

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_platform_probe_worker_service import (  # noqa: E402
    run_pixelrag_platform_probe_worker,
)


def main() -> int:
    parser = argparse.ArgumentParser(
        description="執行或 dry-run PixelRAG platform probe worker。"
    )
    parser.add_argument(
        "--artifact-root",
        help="PixelRAG visual evidence artifact root；預設使用 production/container 設定。",
    )
    parser.add_argument(
        "--vlm-receipt-root",
        help="PixelRAG VLM replay receipt root；預設使用 production/container 設定。",
    )
    parser.add_argument(
        "--output-root",
        help="Platform probe worker receipt output root。",
    )
    parser.add_argument(
        "--capture-output-root",
        help="Public-context capture artifact output root；預設寫回 PixelRAG visual evidence root。",
    )
    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=25,
        help="最多處理 probe candidate 數。",
    )
    parser.add_argument(
        "--timeout",
        type=int,
        default=30,
        help="public-context capture timeout 秒數。",
    )
    parser.add_argument(
        "--settle-ms",
        type=int,
        default=800,
        help="頁面載入後等待毫秒數。",
    )
    parser.add_argument(
        "--max-tiles",
        type=int,
        default=12,
        help="每次 public-context capture 最多切出的 tiles。",
    )
    parser.add_argument(
        "--execute",
        action="store_true",
        help="真的執行 public-context capture 或 structured fallback package；未指定時只做 no-write dry-run。",
    )
    parser.add_argument(
        "--write-receipt",
        action="store_true",
        help="execute 後寫入 platform probe worker artifact receipt；不寫 DB。",
    )
    args = parser.parse_args()

    payload = run_pixelrag_platform_probe_worker(
        artifact_root=args.artifact_root,
        vlm_receipt_root=args.vlm_receipt_root,
        output_root=args.output_root,
        capture_output_root=args.capture_output_root,
        platform=tuple(args.platforms or ()),
        max_age_hours=args.max_age_hours,
        limit=args.limit,
        timeout=args.timeout,
        settle_ms=args.settle_ms,
        max_tiles=args.max_tiles,
        execute=args.execute,
        write_receipt=bool(args.write_receipt and args.execute),
    )
    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())
