"""Tests for Kamil ADC neutral preprocessing-set generation."""

from __future__ import annotations

import unittest

import numpy as np

from python_app.models.run_config_model import RunConfigModel
from python_app.workflows.kamil_adc_neutral_preprocess import build_kamil_adc_neutral_s21_sets


class KamilAdcNeutralPreprocessTest(unittest.TestCase):
    def test_builds_passthrough_s21_sets_for_current_sweep(self) -> None:
        config = RunConfigModel.from_dict(
            {
                "radar": {
                    "model": "kamil_adc",
                    "sweep": {
                        "start_hz": 1_000_000.0,
                        "stop_hz": 4_000_000.0,
                        "if_bandwidth_hz": 1.0,
                        "stimulus_power_dbm": -10.0,
                    },
                },
                "switches": {
                    "port1": {"positions": 1},
                    "port2": {"positions": 2},
                },
                "run": {
                    "combos": [
                        {"input": 0, "output": 0},
                        {"input": 1, "output": 0},
                    ],
                },
            }
        )

        calibration, reference = build_kamil_adc_neutral_s21_sets(config, point_count=4)

        expected_frequency = np.linspace(1_000_000.0, 4_000_000.0, 4, dtype=np.float32)
        self.assertEqual(len(calibration.traces), 2)
        self.assertEqual(len(reference.traces), 2)
        self.assertEqual(
            [(trace.combo.input, trace.combo.output) for trace in calibration.traces],
            [(0, 0), (1, 0)],
        )
        for trace in calibration.traces:
            np.testing.assert_array_equal(trace.frequency_hz, expected_frequency)
            np.testing.assert_array_equal(trace.s11, np.zeros(4, dtype=np.complex64))
            np.testing.assert_array_equal(trace.s21, np.ones(4, dtype=np.complex64))
        for trace in reference.traces:
            np.testing.assert_array_equal(trace.frequency_hz, expected_frequency)
            np.testing.assert_array_equal(trace.s11, np.zeros(4, dtype=np.complex64))
            np.testing.assert_array_equal(trace.s21, np.zeros(4, dtype=np.complex64))

    def test_rejects_non_kamil_config(self) -> None:
        config = RunConfigModel.from_dict({"radar": {"model": "librevna"}})

        with self.assertRaisesRegex(ValueError, "kamil_adc"):
            build_kamil_adc_neutral_s21_sets(config, point_count=4)


if __name__ == "__main__":
    unittest.main()
