"""Unit tests for BLE LED controller wire protocols.

These tests exercise the pure byte-encoding functions for each family,
so they run without ``bleak`` installed and without any real BLE hardware.
"""

import pytest

from ledgrab.core.devices.ble_protocols import (
    all_protocols,
    get_protocol,
    identify_family,
    govee,
    sp110e,
    triones,
    zengge,
)


class TestSP110E:
    def test_color_frame_is_four_bytes_with_cmd_tail(self):
        frame = sp110e.encode_color(255, 128, 64)
        assert frame == bytes((255, 128, 64, 0x1E))

    def test_brightness_scales_rgb(self):
        frame = sp110e.encode_color(200, 200, 200, brightness=128)
        # 200 * 128 // 255 == 100
        assert frame == bytes((100, 100, 100, 0x1E))

    def test_brightness_255_is_passthrough(self):
        assert sp110e.encode_color(1, 2, 3, 255) == bytes((1, 2, 3, 0x1E))

    def test_clamps_out_of_range(self):
        assert sp110e.encode_color(-5, 300, 128) == bytes((0, 255, 128, 0x1E))

    def test_power_frames(self):
        assert sp110e.encode_power(True) == bytes((0, 0, 0, 0xAA))
        assert sp110e.encode_power(False) == bytes((0, 0, 0, 0xAB))

    def test_init_handshake_is_defined(self):
        # SP110E silently drops the GATT link within ~1s of connect unless
        # this two-write handshake arrives — see module docstring.
        assert len(sp110e.PROTOCOL.init_writes) == 2
        (ffe2_uuid, ffe2_payload), (ffe1_uuid, ffe1_payload) = sp110e.PROTOCOL.init_writes
        assert ffe2_uuid.endswith("ffe2-0000-1000-8000-00805f9b34fb") or "ffe2" in ffe2_uuid
        assert ffe2_payload == b"\x01\x00"
        assert ffe1_uuid == sp110e.PROTOCOL.write_char_uuid
        assert ffe1_payload == b"\x01\xb7\xe3\xd5"


class TestTriones:
    def test_color_frame_matches_documented_template(self):
        # 7E 07 05 03 RR GG BB 10 EF
        frame = triones.encode_color(0xAA, 0xBB, 0xCC)
        assert frame == bytes((0x7E, 0x07, 0x05, 0x03, 0xAA, 0xBB, 0xCC, 0x10, 0xEF))

    def test_frame_is_nine_bytes(self):
        assert len(triones.encode_color(10, 20, 30)) == 9

    def test_brightness_scales(self):
        frame = triones.encode_color(255, 255, 255, brightness=51)  # 20%
        # 255 * 51 // 255 == 51
        assert frame[4:7] == bytes((51, 51, 51))

    def test_power_on_off_distinct(self):
        on = triones.encode_power(True)
        off = triones.encode_power(False)
        assert on != off
        assert on[0] == 0x7E and off[0] == 0x7E
        assert on[-1] == 0xEF and off[-1] == 0xEF


class TestZengge:
    def test_color_frame_format(self):
        # 56 RR GG BB 00 F0 AA
        frame = zengge.encode_color(0x11, 0x22, 0x33)
        assert frame == bytes((0x56, 0x11, 0x22, 0x33, 0x00, 0xF0, 0xAA))

    def test_frame_is_seven_bytes(self):
        assert len(zengge.encode_color(1, 2, 3)) == 7

    def test_power_frames(self):
        assert zengge.encode_power(True) == bytes((0xCC, 0x23, 0x33))
        assert zengge.encode_power(False) == bytes((0xCC, 0x24, 0x33))


class TestGovee:
    def test_color_frame_is_twenty_bytes_with_checksum(self):
        frame = govee.encode_color(255, 0, 0)
        assert len(frame) == 20
        # Verify XOR checksum
        expected_checksum = 0
        for i in range(19):
            expected_checksum ^= frame[i]
        assert frame[19] == expected_checksum

    def test_color_frame_header(self):
        # 33 05 02 RR GG BB ...
        frame = govee.encode_color(0x10, 0x20, 0x30)
        assert frame[0] == 0x33
        assert frame[1] == 0x05
        assert frame[2] == 0x02
        assert frame[3:6] == bytes((0x10, 0x20, 0x30))

    def test_brightness_scales(self):
        frame = govee.encode_color(100, 100, 100, brightness=0)
        assert frame[3:6] == bytes((0, 0, 0))

    def test_power_frames_have_valid_checksum(self):
        for state in (True, False):
            frame = govee.encode_power(state)
            assert len(frame) == 20
            checksum = 0
            for i in range(19):
                checksum ^= frame[i]
            assert frame[19] == checksum


class TestRegistry:
    def test_all_four_families_registered(self):
        families = set(all_protocols().keys())
        assert families == {"sp110e", "triones", "zengge", "govee"}

    def test_get_protocol_raises_on_unknown(self):
        with pytest.raises(ValueError, match="Unknown BLE family"):
            get_protocol("not-a-real-family")

    def test_get_protocol_returns_correct_family(self):
        assert get_protocol("sp110e").family == "sp110e"
        assert get_protocol("triones").family == "triones"

    def test_identify_family_by_name(self):
        assert identify_family("SP110E_A1B2C3") == "sp110e"
        assert identify_family("Triones-ABCDEF") == "triones"
        assert identify_family("Zengge-123456") == "zengge"
        assert identify_family("ihoment_H6008_xxxx") == "govee"

    def test_identify_family_returns_none_for_unknown(self):
        assert identify_family("SomeRandomDevice") is None
        assert identify_family("") is None
