"""Raw acquisition producer for matrix-mode radars (LibreVNA multi-device, SN9000)."""

from __future__ import annotations

import argparse
import logging
from pathlib import Path
import signal
import threading
import time

from python_app.hardware_full.matrix_radar_service import create_matrix_radar_service
from python_app.models.run_config_model import RunConfigModel
from python_app.orchestration.shm import ShmRingWriter
from python_app.storage.npz.serialize import RAW_MAGIC, serialize_trace_collection

logger = logging.getLogger(__name__)


def main() -> int:
    """Run producer process until config or signal requests exit."""
    parser = argparse.ArgumentParser(description="Publish matrix-radar raw sweeps to SHM rings")
    parser.add_argument("--config", required=True, type=Path, help="Path to run_config.json")
    args = parser.parse_args()

    logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s: %(message)s")
    stop_requested = threading.Event()

    def request_stop(_signum: int, _frame: object) -> None:
        stop_requested.set()

    signal.signal(signal.SIGINT, request_stop)
    signal.signal(signal.SIGTERM, request_stop)

    config = RunConfigModel.load_from_path(args.config)
    config.apply_device_model_constraints()
    if not config.is_matrix_radar:
        raise RuntimeError(
            "matrix_raw_producer requires a matrix-mode radar.model "
            "(librevna_multi or sn9000)"
        )

    raw_writer = ShmRingWriter(
        config.rings.raw.name,
        config.rings.raw.capacity,
        config.rings.raw.slot_size_bytes,
    )
    raw_tap_writer = ShmRingWriter(
        config.rings.raw_tap.name,
        config.rings.raw_tap.capacity,
        config.rings.raw_tap.slot_size_bytes,
    )
    radar = create_matrix_radar_service(config)

    try:
        radar.open()
        radar.configure(config.radar.sweep)
        collection_id = 1
        while not stop_requested.is_set():
            collection_start = time.monotonic()
            collection = radar.acquire_collection(collection_id=collection_id)

            payload = serialize_trace_collection(collection, RAW_MAGIC)
            if not raw_writer.push(payload):
                raise RuntimeError(
                    f"Raw payload size {len(payload)} exceeds ring slot size {raw_writer.slot_size_bytes}"
                )
            if not raw_tap_writer.push(payload):
                raise RuntimeError(
                    f"Raw tap payload size {len(payload)} exceeds ring slot size {raw_tap_writer.slot_size_bytes}"
                )
            if not config.runtime.continuous:
                break
            collection_duration_s = time.monotonic() - collection_start
            if collection_id == 1 or collection_id % 20 == 0 or collection_duration_s > 2.0:
                logger.info(
                    "matrix radar collection %d acquired in %.3f s",
                    collection_id,
                    collection_duration_s,
                )
            collection_id += 1
    finally:
        radar.close()
        raw_tap_writer.close()
        raw_writer.close()

    logger.info("matrix radar raw producer stopped")
    return 0


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