#!/usr/bin/env python3
"""手动增量同步 multi_demand_video_detail（每批查询后立刻写入）。

用法:
    python jobs/sync_multi_demand_videos.py              # 只跑 1 批 100 个
    python jobs/sync_multi_demand_videos.py 100           # 同上
    python jobs/sync_multi_demand_videos.py 100 100       # 从 offset=100 起再跑 1 批
    python jobs/sync_multi_demand_videos.py all           # 循环每批 100，查完写一批直到结束
"""

from __future__ import annotations

import logging
import sys

from supply_infra.scheduler.jobs.sync_multi_demand_videos import (
    VIDEO_SYNC_BATCH_SIZE,
    sync_multi_demand_videos,
)

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


def main(limit_arg: str | None = None, offset_arg: str | None = None) -> dict:
    if limit_arg is None:
        limit: int | None = VIDEO_SYNC_BATCH_SIZE
    elif limit_arg.lower() in {"all", "0", "-1"}:
        limit = None
    else:
        limit = int(limit_arg)

    offset = int(offset_arg) if offset_arg is not None else 0
    result = sync_multi_demand_videos(
        limit=limit,
        offset=offset,
        batch_size=VIDEO_SYNC_BATCH_SIZE,
    )
    print(result)

    remaining = int(result.get("remaining") or 0)
    next_offset = result.get("next_offset")
    if remaining > 0 and next_offset is not None and limit is not None:
        print(
            f"还有 {remaining} 个未处理。下一批:\n"
            f"  python jobs/sync_multi_demand_videos.py {limit} {next_offset}"
        )
    return result


if __name__ == "__main__":
    main(
        sys.argv[1] if len(sys.argv) > 1 else None,
        sys.argv[2] if len(sys.argv) > 2 else None,
    )
