from __future__ import annotations

import argparse
from pathlib import Path

from capture_format_v2 import file_summary, read_capture
from pipeline_common_corrected import parse_imu_capture, parse_rtk_capture, write_json, write_jsonl


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--rtk", type=Path, required=True)
    parser.add_argument("--imu", type=Path, required=True)
    parser.add_argument("--out", type=Path, required=True)
    args = parser.parse_args()
    args.out.mkdir(parents=True, exist_ok=True)
    rtk_capture = read_capture(args.rtk)
    imu_capture = read_capture(args.imu)
    rtk_rows = parse_rtk_capture(rtk_capture)
    imu_rows = parse_imu_capture(imu_capture)
    write_jsonl(args.out / "rtk.jsonl", rtk_rows)
    write_jsonl(args.out / "imu.jsonl", imu_rows)
    write_json(args.out / "parse_summary.json", {
        "rtk_capture": file_summary(rtk_capture),
        "imu_capture": file_summary(imu_capture),
        "rtk_records": len(rtk_rows),
        "rtk_checksum_valid": sum(bool(row.get("checksum_valid")) for row in rtk_rows),
        "imu_frames": len(imu_rows),
        "imu_crc_valid": sum(bool(row.get("crc_valid")) for row in imu_rows),
    })
    print(f"RTK records={len(rtk_rows)}, IMU frames={len(imu_rows)}")


if __name__ == "__main__":
    main()
