"""Tests for PAIR + PQTM command builders."""

from lc29h.commands import (
    pair_cold_start,
    pair_factory_reset,
    pair_get_version,
    pair_hot_start,
    pair_save_config,
    pair_set_baud_rate,
    pair_set_constellation,
    pair_set_fix_rate,
    pair_warm_start,
    pqtm_cfg_survey_in,
    pqtm_restart,
    pqtm_save_params,
    pqtm_version,
)
from lc29h.protocol import validate_checksum


def _decode(cmd: bytes) -> str:
    """Decode command bytes and strip CRLF."""
    return cmd.decode("ascii").strip()


class TestSystemCommands:
    def test_cold_start(self):
        cmd = _decode(pair_cold_start())
        assert cmd == "$PAIR003*39"
        assert validate_checksum(cmd)

    def test_hot_start(self):
        cmd = _decode(pair_hot_start())
        assert validate_checksum(cmd)
        assert "PAIR004" in cmd

    def test_warm_start(self):
        cmd = _decode(pair_warm_start())
        assert validate_checksum(cmd)
        assert "PAIR005" in cmd

    def test_factory_reset(self):
        cmd = _decode(pair_factory_reset())
        assert validate_checksum(cmd)
        assert "PAIR007" in cmd

    def test_get_version(self):
        cmd = _decode(pair_get_version())
        assert validate_checksum(cmd)
        assert "PAIR021" in cmd


class TestConfigCommands:
    def test_set_fix_rate(self):
        cmd = _decode(pair_set_fix_rate(1000))
        assert validate_checksum(cmd)
        assert "PAIR050,1000" in cmd

    def test_set_fix_rate_10hz(self):
        cmd = _decode(pair_set_fix_rate(100))
        assert validate_checksum(cmd)
        assert "PAIR050,100" in cmd

    def test_set_baud_rate(self):
        cmd = _decode(pair_set_baud_rate(115200))
        assert validate_checksum(cmd)
        assert "PAIR051,115200" in cmd

    def test_constellation_all(self):
        cmd = _decode(pair_set_constellation(True, True, True, True, True))
        assert validate_checksum(cmd)
        assert "PAIR066,1,1,1,1,1" in cmd

    def test_constellation_gps_only(self):
        cmd = _decode(pair_set_constellation(True, False, False, False, False))
        assert validate_checksum(cmd)
        assert "PAIR066,1,0,0,0,0" in cmd

    def test_save_config(self):
        cmd = _decode(pair_save_config())
        assert validate_checksum(cmd)
        assert "PAIR513" in cmd


class TestPQTMCommands:
    def test_version(self):
        cmd = _decode(pqtm_version())
        assert validate_checksum(cmd)
        assert "PQTMVERNO" in cmd

    def test_save_params(self):
        cmd = _decode(pqtm_save_params())
        assert validate_checksum(cmd)
        assert "PQTMSAVEPAR" in cmd

    def test_restart_hot(self):
        cmd = _decode(pqtm_restart(1))
        assert validate_checksum(cmd)
        assert "PQTMGNSSRESTART,1" in cmd

    def test_restart_cold(self):
        cmd = _decode(pqtm_restart(3))
        assert validate_checksum(cmd)
        assert "PQTMGNSSRESTART,3" in cmd

    def test_survey_in(self):
        cmd = _decode(pqtm_cfg_survey_in(mode=1, duration_s=3600, accuracy_m=1.0))
        assert validate_checksum(cmd)
        assert "PQTMCFGSVIN,W,1,3600" in cmd
