import pandas as pd
import numpy as np
import pytest
from backend.Property import Property
from etl.epc.Record import EPCRecord
from recommendations.tests.test_data.materials import materials
from recommendations.SolarPvRecommendations import SolarPvRecommendations


class TestSolarPvRecommendations:
    @pytest.fixture
    def property_instance_invalid_type(self):
        # Setup the property_instance with an invalid property type
        epc_record = EPCRecord()
        epc_record.property_type = "InvalidType"
        epc_record.county = "Broxbourne"
        epc_record.photo_supply = None
        property_instance_invalid_type = Property(id=1, address="", postcode="", epc_record=epc_record)
        property_instance_invalid_type.roof = {"is_flat": False, "is_pitched": False, "is_roof_room": False}
        property_instance_invalid_type.already_installed = []
        return property_instance_invalid_type

    @pytest.fixture
    def property_instance_invalid_roof(self):
        # Setup the property_instance with invalid roof type
        epc_record = EPCRecord()
        epc_record.county = "Huntingdonshire"
        epc_record.property_type = "House"
        epc_record.photo_supply = None
        property_instance_invalid_roof = Property(id=1, address="", postcode="", epc_record=epc_record)
        property_instance_invalid_roof.roof = {
            "is_flat": False, "is_pitched": False, "is_roof_room": False, "thermal_transmittance": None
        }
        property_instance_invalid_roof.already_installed = []
        return property_instance_invalid_roof

    @pytest.fixture
    def property_instance_has_solar_pv(self):
        # Setup the property_instance with existing solar pv
        epc_record = EPCRecord()
        epc_record.photo_supply = 40.0  # Use float, not string
        epc_record.county = "Huntingdonshire"
        epc_record.property_type = "House"
        property_instance_has_solar_pv = Property(id=1, address="", postcode="", epc_record=epc_record)
        property_instance_has_solar_pv.roof = {"is_flat": True, "thermal_transmittance": None}
        property_instance_has_solar_pv.already_installed = []
        return property_instance_has_solar_pv

    @pytest.fixture
    def property_instance_valid_all(self):
        # Setup a valid property_instance that passes all conditions
        epc_record = EPCRecord()
        epc_record.property_type = "House"
        epc_record.photo_supply = None
        epc_record.county = "Huntingdonshire"
        property_instance_valid_all = Property(id=1, address="", postcode="", epc_record=epc_record)
        property_instance_valid_all.roof_area = 40
        property_instance_valid_all.number_of_floors = 2
        property_instance_valid_all.roof = {"is_flat": True, "thermal_transmittance": None}
        property_instance_valid_all.already_installed = []
        property_instance_valid_all.solar_panel_configuration = {
            "panel_performance": pd.DataFrame(
                [
                    {
                        "panneled_roof_area": 20,
                        "n_panels": 10,
                        "array_wattage": 4000,
                        "initial_ac_kwh_per_year": 3800
                    }
                ]
            )
        }

        return property_instance_valid_all

    def test_invalid_property_type(self, property_instance_invalid_type):
        solar_pv = SolarPvRecommendations(property_instance_invalid_type, materials=materials)
        solar_pv.recommend(phase=0)
        assert not solar_pv.recommendation

    def test_invalid_roof_type(self, property_instance_invalid_roof):
        solar_pv = SolarPvRecommendations(property_instance_invalid_roof, materials=materials)
        solar_pv.recommend(phase=0)
        assert not solar_pv.recommendation

    def test_existing_solar_pv(self, property_instance_has_solar_pv):
        solar_pv = SolarPvRecommendations(property_instance_has_solar_pv, materials=materials)
        solar_pv.recommend(phase=0)
        assert not solar_pv.recommendation

    def test_valid_all_conditions(self, property_instance_valid_all):
        solar_pv = SolarPvRecommendations(property_instance_valid_all, materials=materials)
        solar_pv.recommend(phase=0)
        assert len(solar_pv.recommendation) == 11
        assert solar_pv.recommendation[0]["description"] == '10 panel system, 400W solar panels - 4.0 kWp system'
        assert not solar_pv.recommendation[0]["has_battery"]
        assert solar_pv.recommendation[0]["initial_ac_kwh_per_year"] == np.int64(3800)
        assert solar_pv.recommendation[0]["description_simulation"] == {'photo-supply': np.float64(50.0)}
        assert solar_pv.recommendation[0]["simulation_config"] == {'photo_supply_ending': np.float64(50.0)}

        assert (
            solar_pv.recommendation[1]["description"] ==
            '10 panel system, 400W solar panels, 5.8kw Growatt battery - 4.0 kWp system'
        )
        assert solar_pv.recommendation[1]["has_battery"]
