import pytest
from datetime import date

from backend.condition.persistence.condition_postgres import ConditionPostgres
from backend.condition.domain.property_condition_survey import PropertyConditionSurvey
from backend.condition.domain.element import Element
from backend.condition.domain.element_type import ElementType
from backend.condition.domain.aspect_condition import AspectCondition
from backend.condition.domain.aspect_type import AspectType
from backend.app.db.models.condition import PropertyConditionSurveyModel
from backend.condition.tests.custom_asserts import CustomAsserts


def test_map_survey_to_model() -> None:
    # arrange
    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,
                    )
                ],
            ),
            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),
        source="Peabody",
    )

    expected = {
        "uprn": 1,
        "date": date(2000, 1, 1),
        "source": "Peabody",
        "elements": [
            {
                "element_type": ElementType.EXTERNAL_WINDOWS,
                "element_instance": 1,
                "aspects": [
                    {
                        "aspect_type": AspectType.MATERIAL,
                        "aspect_instance": 1,
                        "value": "UPVC Double Glazed",
                        "quantity": 8,
                        "install_date": None,
                        "renewal_year": 2036,
                        "comments": None,
                    }
                ],
            },
            {
                "element_type": ElementType.EXTERNAL_DECORATION,
                "element_instance": 1,
                "aspects": [
                    {
                        "aspect_type": AspectType.CONDITION,
                        "aspect_instance": 1,
                        "value": "Normal",
                        "quantity": 1,
                        "install_date": None,
                        "renewal_year": 2029,
                        "comments": None,
                    }
                ],
            },
            {
                "element_type": ElementType.EXTERNAL_WALL,
                "element_instance": 1,
                "aspects": [
                    {
                        "aspect_instance": 1,
                        "value": "Pointed",
                        "quantity": 65,
                        "install_date": None,
                        "renewal_year": 2045,
                        "comments": None,
                    },
                    {
                        "aspect_type": AspectType.FINISH,
                        "aspect_instance": 1,
                        "value": "Pointing",
                        "quantity": 1,
                        "install_date": None,
                        "renewal_year": 2069,
                        "comments": None,
                    },
                    {
                        "aspect_type": AspectType.FINISH,
                        "aspect_instance": 2,
                        "value": "Tile Hung",
                        "quantity": 8,
                        "install_date": None,
                        "renewal_year": 2049,
                        "comments": None,
                    },
                ],
            },
        ],
    }

    # act
    model: PropertyConditionSurveyModel = ConditionPostgres.map_survey_to_model(survey)

    # assert (survey level)
    CustomAsserts.assert_property_condition_survey_model_matches_expected(
        model,
        expected,
    )
