"""Tests for register parsers."""

from aiovictron.registers.base import RegisterDefinition, RegisterType
from aiovictron.registers.solar_charger import SolarChargerParser
from aiovictron.registers.system import SystemParser
from aiovictron.registers.vebus import VEBusParser


class TestRegisterDefinition:
    def test_defaults(self):
        reg = RegisterDefinition("test", 100)
        assert reg.register_type == RegisterType.UINT16
        assert reg.scale == 1
        assert reg.count == 1

    def test_int16(self):
        reg = RegisterDefinition("test", 100, RegisterType.INT16, scale=10)
        assert reg.register_type == RegisterType.INT16
        assert reg.scale == 10


class TestSystemParser:
    def setup_method(self):
        self.parser = SystemParser()

    def test_device_type(self):
        assert self.parser.device_type == "system"

    def test_register_count(self):
        assert len(self.parser.registers) == 8

    def test_parse_battery_voltage(self):
        # Register 840, scale /10 → 265 raw = 26.5V
        register_map = {
            817: 0, 820: 120, 840: 265, 841: 0xFFE0,  # -32 signed
            842: 0xFF56,  # -170 signed
            843: 78, 844: 4, 850: 350,
        }
        result = self.parser.parse(register_map)
        assert result["battery_voltage"] == 26.5
        assert result["battery_current"] == -3.2  # -32 / 10
        assert result["battery_power"] == -170.0
        assert result["battery_soc"] == 78.0
        assert result["pv_power"] == 350.0
        assert result["ac_consumption_l1"] == 120.0

    def test_register_ranges(self):
        ranges = self.parser.register_ranges
        # Should coalesce into at most 2 blocks
        assert len(ranges) >= 1
        assert len(ranges) <= 3


class TestSolarChargerParser:
    def setup_method(self):
        self.parser = SolarChargerParser()

    def test_device_type(self):
        assert self.parser.device_type == "solar_charger"

    def test_register_count(self):
        assert len(self.parser.registers) == 8

    def test_parse_pv_voltage(self):
        # Register 776, scale /100 → 3850 raw = 38.50V
        register_map = {
            771: 3, 774: 2680, 775: 125, 776: 3850,
            777: 91, 784: 18, 789: 350, 790: 420,
        }
        result = self.parser.parse(register_map)
        assert result["pv_voltage"] == 38.50
        assert result["battery_voltage"] == 26.80
        assert result["pv_current"] == 9.1  # 91 / 10
        assert result["yield_today"] == 1.8  # 18 / 10
        assert result["charger_state"] == 3.0


class TestVEBusParser:
    def setup_method(self):
        self.parser = VEBusParser()

    def test_device_type(self):
        assert self.parser.device_type == "vebus"

    def test_register_count(self):
        assert len(self.parser.registers) == 14

    def test_parse_ac_voltages(self):
        register_map = {
            3: 2401, 6: 25, 7: 5001, 8: 600,
            12: 2400, 13: 31, 14: 5000, 15: 744,
            26: 2650, 27: 0xFFB0,  # -80 signed = -8.0A
            30: 225, 31: 780, 32: 9, 33: 0xFF38,  # -200 signed
        }
        result = self.parser.parse(register_map)
        assert result["ac_input_voltage"] == 240.1
        assert result["ac_input_frequency"] == 50.01
        assert result["ac_output_voltage"] == 240.0
        assert result["battery_voltage"] == 26.50
        assert result["battery_current"] == -8.0
        assert result["battery_temperature"] == 22.5
        assert result["battery_soc"] == 78.0
        assert result["dc_power"] == -200.0
        assert result["vebus_state"] == 9.0

    def test_register_ranges(self):
        ranges = self.parser.register_ranges
        # VE.Bus registers span 3-33, should be 1 block
        assert len(ranges) == 1
