"""Factory for single-radar Python acquisition services."""

from __future__ import annotations

from typing import Protocol

from python_app.hardware_full.kamil_adc_service import KamilAdcService
from python_app.hardware_full.librevna_driver.models import SweepResult
from python_app.hardware_full.librevna_service import LibreVnaService
from python_app.hardware_full.remote_compact_m_k209_service import RemoteCompactMK209Service
from python_app.models.run_config_model import RadarSweepModel, RunConfigModel


class SingleRadarService(Protocol):
    """Common API used by single-radar workflows."""

    def open(self) -> None:
        """Open radar connection."""

    def close(self) -> None:
        """Close radar connection."""

    def configure(self, sweep: RadarSweepModel) -> None:
        """Apply sweep settings."""

    def read_device_limits(self) -> dict[str, float | int]:
        """Read device capability limits."""

    def acquire(self) -> SweepResult:
        """Acquire one sweep."""


def create_single_radar_service(config: RunConfigModel) -> SingleRadarService:
    """Create the Python service for a non-multi-device radar config."""
    if config.is_multi_device:
        raise RuntimeError("single-radar service factory does not support librevna_multi")

    model = config.radar.model or RunConfigModel.LIBREVNA_MODEL
    if model == RunConfigModel.LIBREVNA_MODEL:
        return LibreVnaService(serial=config.radar.serial or None)

    if model == RunConfigModel.COMPACT_M_K209_MODEL:
        if config.radar.driver_mode != "native":
            raise RuntimeError("Compact-M K209 requires radar.driver_mode='native'")
        return RemoteCompactMK209Service(
            host=config.radar.remote_host,
            port=config.radar.remote_port,
        )

    if model == RunConfigModel.KAMIL_ADC_MODEL:
        if config.radar.driver_mode != "native":
            raise RuntimeError("Kamil ADC requires radar.driver_mode='native'")
        return KamilAdcService(config)

    raise RuntimeError(f"Unsupported single-radar model: {model}")
