import pytest
from etl.epc_clean.epc_attributes.MainheatAttributes import MainHeatAttributes
from etl.epc_clean.tests.test_data.test_mainheat_attributes_cases import mainheat_cases


class TestMainHeatAttributes:

    def test_init(self):
        # Test initialization with a valid description
        valid_description = 'Room heaters, electric, Room heaters, mains gas'
        floor_attr = MainHeatAttributes(valid_description)
        assert floor_attr.description == valid_description.lower()

        # Test initialization with a description that contains none of the keywords
        with pytest.raises(ValueError):
            MainHeatAttributes('description without keywords')

    def test_empty_str(self):
        assert MainHeatAttributes("").process() == {
            'has_radiators': False, 'has_fan_coil_units': False, 'has_pipes_in_screed_above_insulation': False,
            'has_pipes_in_insulated_timber_floor': False, 'has_pipes_in_concrete_slab': False, 'has_boiler': False,
            'has_air_source_heat_pump': False, 'has_room_heaters': False, 'has_electric_storage_heaters': False,
            'has_warm_air': False, 'has_electric_underfloor_heating': False, 'has_electric_ceiling_heating': False,
            'has_community_scheme': False, 'has_ground_source_heat_pump': False, 'has_no_system_present': False,
            'has_portable_electric_heaters': False, 'has_water_source_heat_pump': False,
            'has_electric_heat_pump': False, 'has_micro-cogeneration': False, 'has_solar_assisted_heat_pump': False,
            'has_exhaust_source_heat_pump': False, 'has_community_heat_pump': False, 'has_hot-water-only': False,
            'has_electric': False, 'has_mains_gas': False, 'has_wood_logs': False, 'has_coal': False, 'has_oil': False,
            'has_wood_pellets': False, 'has_anthracite': False, 'has_dual_fuel_mineral_and_wood': False,
            'has_smokeless_fuel': False, 'has_lpg': False, 'has_b30k': False, 'has_mineral_and_wood': False,
            'has_dual_fuel_appliance': False, 'has_wood_chips': False, 'has_assumed': False, 'has_electricaire': False,
            'has_assumed_for_most_rooms': False, 'has_underfloor_heating': False
        }

        assert set(list(MainHeatAttributes("").process().values())) == {False}

    @pytest.mark.parametrize(
        "test_case",
        mainheat_cases
    )
    def test_process_mainheat(self, test_case):
        expected_result = test_case.copy()
        del expected_result["original_description"]
        result = MainHeatAttributes(test_case['original_description']).process()

        # Some of the expected_result test data was produced before some attributes were added to the code
        # base so we need to filter out some of the keys. The test is still valid
        result = {k: v for k, v in result.items() if v}
        expected_result = {k: v for k, v in expected_result.items() if v}

        assert sorted(result.items()) == sorted(expected_result.items())

    def test_invalid_description(self):
        # Test that invalid descriptions raise a ValueError
        invalid_descriptions = [
            "invalid description",
            "description with no known heating data_types",
        ]

        for description in invalid_descriptions:
            with pytest.raises(ValueError):
                MainHeatAttributes(description).process()
