#!/usr/bin/env python3
from __future__ import annotations

import argparse
import json
from pathlib import Path

from k1link.compute import (
    DEFAULT_GROUND_BENCHMARK_PROFILE,
    PATCHWORKPP_SOURCE_COMMIT,
    PATCHWORKPP_SOURCE_TAG,
    GroundBenchmarkProfile,
    LidarGroundBenchmarkV1,
    LidarReplayPackV2,
    PatchworkPPGroundSegmenter,
    build_lidar_ground_annotation_template,
    build_lidar_ground_benchmark,
)


def _arguments() -> argparse.Namespace:
    parser = argparse.ArgumentParser(
        description=(
            "Run diagnostic-only local-percentile vs Patchwork++ ground A/B "
            "against one immutable LiDAR replay pack."
        )
    )
    parser.add_argument("replay_pack", type=Path)
    parser.add_argument("output_root", type=Path)
    parser.add_argument(
        "--annotation-root",
        type=Path,
        help="Optional root for an all-ignore human-review template.",
    )
    parser.add_argument(
        "--annotation-frames",
        type=int,
        default=8,
    )
    parser.add_argument(
        "--patchwork-module",
        default="pypatchworkpp",
    )
    parser.add_argument(
        "--patchwork-source-tag",
        default=PATCHWORKPP_SOURCE_TAG,
    )
    parser.add_argument(
        "--patchwork-source-commit",
        default=PATCHWORKPP_SOURCE_COMMIT,
    )
    parser.add_argument(
        "--profile-id",
        default=DEFAULT_GROUND_BENCHMARK_PROFILE.profile_id,
    )
    parser.add_argument(
        "--sensor-height-m",
        type=float,
        default=DEFAULT_GROUND_BENCHMARK_PROFILE.patchwork_sensor_height_proxy_m,
    )
    parser.add_argument(
        "--map-vertical-origin-offset-m",
        type=float,
        default=(DEFAULT_GROUND_BENCHMARK_PROFILE.patchwork_map_vertical_origin_offset_m),
    )
    parser.add_argument(
        "--height-evidence",
        choices=("missing", "operator-estimated", "runtime-calibrated"),
        default=DEFAULT_GROUND_BENCHMARK_PROFILE.patchwork_height_evidence,
    )
    return parser.parse_args()


def main() -> int:
    arguments = _arguments()
    profile = GroundBenchmarkProfile(
        profile_id=arguments.profile_id,
        patchwork_sensor_height_proxy_m=arguments.sensor_height_m,
        patchwork_map_vertical_origin_offset_m=(arguments.map_vertical_origin_offset_m),
        patchwork_height_evidence=arguments.height_evidence,
    )
    replay = LidarReplayPackV2(arguments.replay_pack)
    try:
        patchwork = PatchworkPPGroundSegmenter.load(
            profile=profile,
            module_name=arguments.patchwork_module,
            source_tag=arguments.patchwork_source_tag,
            source_commit=arguments.patchwork_source_commit,
        )
        output = build_lidar_ground_benchmark(
            replay,
            arguments.output_root,
            patchwork=patchwork,
            profile=profile,
        )
        annotation = (
            build_lidar_ground_annotation_template(
                replay,
                arguments.annotation_root,
                requested_frames=arguments.annotation_frames,
            )
            if arguments.annotation_root is not None
            else None
        )
    finally:
        replay.close()

    result = LidarGroundBenchmarkV1(output)
    try:
        print(
            json.dumps(
                {
                    "benchmark_id": result.benchmark_id,
                    "status": result.report["status"],
                    "decision": result.report["decision"],
                    "current": result.report["current"],
                    "candidate": result.report["candidate"],
                    "comparison": result.report["comparison"],
                    "annotation_template_id": (annotation.name if annotation is not None else None),
                },
                ensure_ascii=False,
                indent=2,
            )
        )
    finally:
        result.close()
    return 0


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