"""Backfill helper: split a Mixpanel Export API file into <=500-event chunks.

Mixpanel's raw export is already newline-delimited ``{"event", "properties"}``
JSON with ``time`` in epoch seconds and ``$insert_id`` — exactly what the v1
catalog reads — so lines are copied verbatim, 500 per chunk file.

Usage: python3 split_ndjson.py <export.ndjson> <out-dir>
"""

import sys
from pathlib import Path

CHUNK = 500  # the receiver's per-request cap


def main(source: str, out_dir: str) -> None:
    lines = [line for line in Path(source).read_text().splitlines() if line.strip()]
    target = Path(out_dir)
    target.mkdir(parents=True, exist_ok=True)
    for index in range(0, len(lines), CHUNK):
        body = "\n".join(lines[index : index + CHUNK]) + "\n"
        (target / f"chunk-{index // CHUNK:05d}.ndjson").write_text(body)
    print(f"backfill: {len(lines)} events in {(len(lines) + CHUNK - 1) // CHUNK} chunk(s)")


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