from __future__ import annotations
from dataclasses import replace
from typing import Optional

from backend.app.db.models.recommendations import ScenarioModel
from backend.app.domain.records.scenario_record import ScenarioRecord


class Scenario:
    def __init__(self, record: ScenarioRecord, id: Optional[int] = None):
        self.id = id
        self.record = record

    @classmethod
    def from_sqlalchemy(cls, scenario_model: ScenarioModel) -> Scenario:
        record = ScenarioRecord(
            name=scenario_model.name,
            created_at=scenario_model.created_at,
            housing_type=scenario_model.housing_type,
            goal=scenario_model.goal,
            goal_value=scenario_model.goal_value,
            trigger_file_path=scenario_model.trigger_file_path,
            multi_plan=scenario_model.multi_plan,
            is_default=scenario_model.is_default,
            budget=scenario_model.budget,
            already_installed_file_path=scenario_model.already_installed_file_path,
            patches_file_path=scenario_model.patches_file_path,
            non_invasive_recommendations_file_path=scenario_model.non_invasive_recommendations_file_path,
            exclusions=scenario_model.exclusions,
            cost=scenario_model.cost,
            contingency=scenario_model.contingency,
            funding=scenario_model.funding,
            total_work_hours=scenario_model.total_work_hours,
            energy_savings=scenario_model.energy_savings,
            co2_equivalent_savings=scenario_model.co2_equivalent_savings,
            energy_cost_savings=scenario_model.energy_cost_savings,
            epc_breakdown_pre_retrofit=scenario_model.epc_breakdown_pre_retrofit,
            epc_breakdown_post_retrofit=scenario_model.epc_breakdown_post_retrofit,
            number_of_properties=scenario_model.number_of_properties,
            n_units_to_retrofit=scenario_model.n_units_to_retrofit,
            co2_per_unit_pre_retrofit=scenario_model.co2_per_unit_pre_retrofit,
            co2_per_unit_post_retrofit=scenario_model.co2_per_unit_post_retrofit,
            energy_bill_per_unit_pre_retrofit=scenario_model.energy_bill_per_unit_pre_retrofit,
            energy_bill_per_unit_post_retrofit=scenario_model.energy_bill_per_unit_post_retrofit,
            energy_consumption_per_unit_pre_retrofit=scenario_model.energy_consumption_per_unit_pre_retrofit,
            energy_consumption_per_unit_post_retrofit=scenario_model.energy_consumption_per_unit_post_retrofit,
            valuation_improvement_per_unit=scenario_model.valuation_improvement_per_unit,
            cost_per_unit=scenario_model.cost_per_unit,
            cost_per_co2_saved=scenario_model.cost_per_co2_saved,
            cost_per_sap_point=scenario_model.cost_per_sap_point,
            valuation_return_on_investment=scenario_model.valuation_return_on_investment,
            property_valuation_increase=scenario_model.property_valuation_increase,
            labour_days=scenario_model.labour_days,
        )
        return cls(record, scenario_model.id)

    def set_default(self, value: bool) -> None:
        self.record = replace(self.record, is_default=value)
