#!/usr/bin/env python3
"""Prepare immutable E1 preprocessing and frame-selection inputs."""

from __future__ import annotations

import argparse
import json
from pathlib import Path

from k1link.compute.qualification import (
    DEFAULT_QUALIFICATION_FRAME_COUNT,
    prepare_recorded_qualification_slice,
)
from k1link.device_plugins.xgrids_k1.analyze.valid_fov import (
    DEFAULT_EDGE_MARGIN_PIXELS,
    prepare_k1_valid_fov_mask,
)


def _arguments() -> argparse.Namespace:
    parser = argparse.ArgumentParser()
    parser.add_argument("--job-root", type=Path, required=True)
    parser.add_argument("--calibration-root", type=Path, required=True)
    parser.add_argument("--source-id", required=True)
    parser.add_argument("--output-root", type=Path, required=True)
    parser.add_argument(
        "--sample-count",
        type=int,
        default=DEFAULT_QUALIFICATION_FRAME_COUNT,
    )
    parser.add_argument(
        "--edge-margin-pixels",
        type=float,
        default=DEFAULT_EDGE_MARGIN_PIXELS,
    )
    return parser.parse_args()


def main() -> int:
    args = _arguments()
    mask = prepare_k1_valid_fov_mask(
        calibration_snapshot_root=args.calibration_root,
        source_id=args.source_id,
        output_root=args.output_root / "valid-fov",
        edge_margin_pixels=args.edge_margin_pixels,
    )
    qualification = prepare_recorded_qualification_slice(
        job_root=args.job_root,
        output_root=args.output_root / "qualification-slices",
        sample_count=args.sample_count,
    )
    print(
        json.dumps(
            {
                "schema_version": "missioncore.e1-qualification-preparation/v1",
                "valid_fov": {
                    "generation_id": mask.generation_id,
                    "manifest": str(mask.manifest_path),
                    "mask": str(mask.mask_path),
                    "calibration_sha256": mask.calibration_sha256,
                    "source_id": mask.source_id,
                    "calibration_slot": mask.calibration_slot,
                    "center_xy": list(mask.center_xy),
                    "radius_pixels": mask.radius_pixels,
                    "crop_xyxy_exclusive": list(mask.crop_xyxy),
                    "valid_pixel_count": mask.valid_pixel_count,
                    "valid_fraction": mask.valid_fraction,
                },
                "qualification_slice": {
                    "generation_id": qualification.generation_id,
                    "manifest": str(qualification.manifest_path),
                    "job_id": qualification.job_id,
                    "input_sha256": qualification.input_sha256,
                    "policy": qualification.policy,
                    "source_frame_count": qualification.source_frame_count,
                    "selected_frame_count": len(qualification.frames),
                    "first_frame_index": qualification.frames[0].frame_index,
                    "last_frame_index": qualification.frames[-1].frame_index,
                },
            },
            ensure_ascii=False,
            indent=2,
            sort_keys=True,
        )
    )
    return 0


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