from datetime import datetime, date

from backend.condition.domain.aspect_condition import AspectCondition
from backend.condition.domain.aspect_type import AspectType
from backend.condition.domain.element_type import ElementType
from backend.condition.domain.mapping.peabody.peabody_mapper import PeabodyMapper
from backend.condition.domain.property_condition_survey import PropertyConditionSurvey
from backend.condition.parsing.records.peabody.peabody_asset_condition import (
    PeabodyAssetCondition,
)
from backend.condition.parsing.records.peabody.peabody_property import PeabodyProperty
from backend.condition.domain.element import Element
from backend.condition.tests.custom_asserts import CustomAsserts


def test_peabody_mapper_maps_property():
    # arrange
    peabody_property = PeabodyProperty(
        uprn=1,
        assets=[
            PeabodyAssetCondition(
                lo_reference="1000RAND0000",
                full_address="FLAT 1 RANDOM SQUARE FAKE STREET LONDON E1 1EE",
                location_type_code=1,
                parent_lo_reference="RAND1000",
                element_code=130,
                element="WINDOWS",
                sub_element_code=1,
                sub_element="Windows",
                material_code=1,
                material_or_answer="UPVC Double Glazed",
                renewal_quantity=8,
                renewal_year=2036,
                renewal_cost=4800,
                cloned="N",
                lo_type_code=1,
                condition_survey_date=datetime(2024, 2, 15, 12, 47, 0),
            ),
            PeabodyAssetCondition(
                lo_reference="1000RAND0000",
                full_address="FLAT 1 RANDOM SQUARE FAKE STREET LONDON E1 1EE",
                location_type_code=1,
                parent_lo_reference="RAND1000",
                element_code=100,
                element="GENERAL",
                sub_element_code=15,
                sub_element="External Decoration",
                material_code=2,
                material_or_answer="Normal",
                renewal_quantity=1,
                renewal_year=2029,
                renewal_cost=1500,
                cloned="N",
                lo_type_code=1,
                condition_survey_date=datetime(2024, 2, 15, 12, 47, 0),
            ),
        ],
    )
    mapper = PeabodyMapper()

    expected_condition_survey = PropertyConditionSurvey(
        uprn=1,
        elements=[
            Element(
                element_type=ElementType.EXTERNAL_WINDOWS,
                element_instance=1,
                aspect_conditions=[
                    AspectCondition(
                        aspect_type=AspectType.MATERIAL,
                        aspect_instance=1,
                        value="UPVC Double Glazed",
                        quantity=8,
                        install_date=None,
                        renewal_year=2036,
                        comments=None,
                    ),
                ],
            ),
            Element(
                element_type=ElementType.EXTERNAL_DECORATION,
                element_instance=1,
                aspect_conditions=[
                    AspectCondition(
                        aspect_type=AspectType.CONDITION,
                        aspect_instance=1,
                        value="Normal",
                        quantity=1,
                        install_date=None,
                        renewal_year=2029,
                        comments=None,
                    )
                ],
            ),
        ],
        date=date(2000, 1, 1),  # what should this be?
        source="Peabody",
    )

    # act
    actual_condition_survey: PropertyConditionSurvey = (
        mapper.map_asset_conditions_for_property(peabody_property)
    )

    # assert
    assert actual_condition_survey == expected_condition_survey


def test_wall_primary_and_secondary_wall_finish_map_correctly():
    # arrange
    peabody_property = PeabodyProperty(
        uprn=1,
        assets=[
            PeabodyAssetCondition(
                lo_reference="1000RAND0000",
                full_address="FLAT 1 RANDOM SQUARE FAKE STREET LONDON E1 1EE",
                location_type_code=1,
                parent_lo_reference="RAND1000",
                element_code=53,
                element="External",
                sub_element_code=23,
                sub_element="Primary Wall Finish",
                material_code=4,
                material_or_answer="Pointed",
                renewal_quantity=65,
                renewal_year=2045,
                renewal_cost=3835,
                cloned="N",
                lo_type_code=1,
                condition_survey_date=datetime(2024, 2, 15, 12, 47, 0),
            ),
            PeabodyAssetCondition(
                lo_reference="1000RAND0000",
                full_address="FLAT 1 RANDOM SQUARE FAKE STREET LONDON E1 1EE",
                location_type_code=1,
                parent_lo_reference="RAND1000",
                element_code=120,
                element="WALLS",
                sub_element_code=2,
                sub_element="Wall Finish",
                material_code=1,
                material_or_answer="Pointing",
                renewal_quantity=1,
                renewal_year=2069,
                renewal_cost=2450,
                cloned="N",
                lo_type_code=1,
                condition_survey_date=datetime(2014, 2, 15, 12, 47, 0),
            ),
            PeabodyAssetCondition(
                lo_reference="1000RAND0000",
                full_address="FLAT 1 RANDOM SQUARE FAKE STREET LONDON E1 1EE",
                location_type_code=1,
                parent_lo_reference="RAND1000",
                element_code=53,
                element="External",
                sub_element_code=30,
                sub_element="Secondary Wall Finish",
                material_code=8,
                material_or_answer="Tile Hung",
                renewal_quantity=8,
                renewal_year=2049,
                renewal_cost=472,
                cloned="N",
                lo_type_code=1,
                condition_survey_date=datetime(2014, 2, 15, 12, 47, 0),
            ),
        ],
    )
    mapper = PeabodyMapper()

    expected_condition_survey = PropertyConditionSurvey(
        uprn=1,
        elements=[
            Element(
                element_type=ElementType.EXTERNAL_WALL,
                element_instance=1,
                aspect_conditions=[
                    AspectCondition(
                        aspect_type=AspectType.FINISH,
                        aspect_instance=1,
                        value="Pointed",
                        quantity=65,
                        install_date=None,
                        renewal_year=2045,
                        comments=None,
                    ),
                    AspectCondition(
                        aspect_type=AspectType.FINISH,
                        aspect_instance=1,
                        value="Pointing",
                        quantity=1,
                        install_date=None,
                        renewal_year=2069,
                        comments=None,
                    ),
                    AspectCondition(
                        aspect_type=AspectType.FINISH,
                        aspect_instance=2,
                        value="Tile Hung",
                        quantity=8,
                        install_date=None,
                        renewal_year=2049,
                        comments=None,
                    ),
                ],
            ),
        ],
        date=date(2000, 1, 1),  # what should this be?
        source="Peabody",
    )

    # act
    actual_condition_survey: PropertyConditionSurvey = (
        mapper.map_asset_conditions_for_property(peabody_property)
    )

    # assert
    assert CustomAsserts.assert_property_condition_surveys_equal(
        actual_condition_survey, expected_condition_survey
    )
