#!/usr/bin/env python3
"""回填 multi_demand_video_detail 已有行的 url1/url2（从 ODPS 拉取）。

Usage:
  python scripts/backfill_multi_demand_video_urls.py
  python scripts/backfill_multi_demand_video_urls.py --decode-dt 20260729
  python scripts/backfill_multi_demand_video_urls.py --limit 500 --offset 0
  python scripts/backfill_multi_demand_video_urls.py --batch-size 100
"""

from __future__ import annotations

import argparse
import json
import logging
import sys
from pathlib import Path

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

from supply_infra.scheduler.jobs.demand_pool.videos import (  # noqa: E402
    VIDEO_SYNC_BATCH_SIZE,
    backfill_multi_demand_video_urls,
)

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s: %(message)s",
)


def _build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(
        description="回填 multi_demand_video_detail.url1/url2"
    )
    parser.add_argument(
        "--decode-dt",
        help="ODPS dwd_topic_decode_result_di 分区日期 YYYYMMDD，默认昨天",
    )
    parser.add_argument(
        "--limit",
        type=int,
        default=None,
        help="本轮最多处理的 vid 数，默认全部",
    )
    parser.add_argument(
        "--offset",
        type=int,
        default=0,
        help="pending 列表起始偏移，用于分批手工执行",
    )
    parser.add_argument(
        "--batch-size",
        type=int,
        default=VIDEO_SYNC_BATCH_SIZE,
        help=f"每批查询 ODPS 的 vid 数，默认 {VIDEO_SYNC_BATCH_SIZE}",
    )
    return parser


def main(argv: list[str] | None = None) -> int:
    args = _build_parser().parse_args(argv)
    result = backfill_multi_demand_video_urls(
        limit=args.limit,
        offset=args.offset,
        batch_size=args.batch_size,
        decode_dt=args.decode_dt,
    )
    print(json.dumps(result, ensure_ascii=False, indent=2))
    return 0


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