"""Minimal GUI tool for direct LibreVNA raw acquisition checks."""

from __future__ import annotations

import argparse
from pathlib import Path
import signal
import sys

PROJECT_ROOT = Path(__file__).resolve().parents[2]
if str(PROJECT_ROOT) not in sys.path:
    sys.path.insert(0, str(PROJECT_ROOT))

from PyQt6.QtWidgets import QApplication

from python_app.scripts.hardware_raw_orchestrator_test import RawOrchestratorViewer


def main() -> int:
    """Run standalone raw-viewer GUI against a selected run config."""
    parser = argparse.ArgumentParser(
        description="Intermediate test: native VNA acquisition with mock switch drivers"
    )
    parser.add_argument(
        "--config",
        type=Path,
        default=PROJECT_ROOT / "run_config.json",
        help="Path to run config JSON",
    )
    parser.add_argument(
        "--no-reset-ring",
        action="store_true",
        help="Do not unlink existing raw ring name before starting",
    )
    parser.add_argument(
        "--skip-radar-prepare",
        action="store_true",
        help="Skip Python pre-configuration of native radar before orchestrator start",
    )
    parser.add_argument(
        "--strict-radar-prepare",
        action="store_true",
        help="Fail immediately if Python pre-configuration cannot run",
    )
    args = parser.parse_args()

    config_path = args.config.resolve()
    if not config_path.exists():
        raise FileNotFoundError(f"Config file not found: {config_path}")

    app = QApplication(sys.argv)
    viewer = RawOrchestratorViewer(
        config_path=config_path,
        reset_ring=not args.no_reset_ring,
        prepare_radar=not args.skip_radar_prepare,
        strict_prepare=args.strict_radar_prepare,
    )
    viewer.setWindowTitle("Raw Sweep Viewer (VNA native + mock switches)")
    viewer.show()

    def _sig_handler(_signum, _frame):
        """Close viewer gracefully on process signals."""
        viewer.close()

    signal.signal(signal.SIGINT, _sig_handler)
    signal.signal(signal.SIGTERM, _sig_handler)

    return app.exec()


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