"""Backfill helper: Amplitude Export API lines -> <=500-event NDJSON chunks.

Amplitude's export files carry ``event_time`` ("YYYY-MM-DD HH:MM:SS.ffffff",
UTC) and ``$insert_id``; the receiver's v1 catalog reads the live-stream
shape (``time`` in epoch milliseconds, ``insert_id``). This rewrites those
two fields and nothing else, then writes chunk files of at most 500 events.

Usage: python3 export_to_ndjson.py <export.jsonl> <out-dir>
"""

import json
import sys
from datetime import UTC, datetime
from pathlib import Path

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


def normalize(event: dict) -> dict:
    out = dict(event)
    if "time" not in out and "event_time" in out:
        parsed = datetime.strptime(out.pop("event_time"), "%Y-%m-%d %H:%M:%S.%f")
        out["time"] = int(parsed.replace(tzinfo=UTC).timestamp() * 1000)
    if "insert_id" not in out and "$insert_id" in out:
        out["insert_id"] = out.pop("$insert_id")
    return out


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


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