import os
import pandas as pd
import numpy as np
from dotenv import load_dotenv
from etl.find_my_epc.AssetListEpcData import AssetListEpcData
from backend.Funding import Funding
from backend.app.utils import sap_to_epc
from recommendations.recommendation_utils import estimate_external_wall_area

load_dotenv(dotenv_path="backend/.env")
EPC_AUTH_TOKEN = os.getenv("EPC_AUTH_TOKEN")

abs_matrix = pd.read_csv(
    "/Users/khalimconn-kowlessar/Downloads/ECO4 Full Project Scores Matrix.csv"
)
pps_matrix = pd.read_excel(
    "/Users/khalimconn-kowlessar/Downloads/ECO4 Partial Project Scores Matrix v5.xlsx",
    header=1
)
pps_matrix.columns = [c.strip() for c in pps_matrix.columns]

asset_list = pd.read_excel(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/ACIS/Solid Wall Properties - Standardised_2.xlsx",
    sheet_name="Standardised Asset List"
)

asset_list = asset_list.rename(
    columns={"domna_address_1": "address", "domna_postcode": "postcode"}
)
asset_list["address"] = asset_list["address"].astype(str)

# Pull the find my EPC data and get the SAP points for solid wall
asset_list_epc_client = AssetListEpcData(
    asset_list=asset_list,
    epc_auth_token=EPC_AUTH_TOKEN
)
asset_list_epc_client.get_data()
asset_list_epc_client.get_non_invasive_recommendations()
# We pull out solid wall insulation
solid_wall_sap_points = []
for r in asset_list_epc_client.non_invasive_recommendations:
    solid_recommendations = [
        x for x in r["recommendations"] if ("internal_wall_insulation" in x["type"]) or (
            "external_wall_insulation" in x["type"]
        )
    ]
    if solid_recommendations:
        solid_recommendations = solid_recommendations[0]
    else:
        continue

    address = r["address"]
    postcode = r["postcode"]

    solid_wall_sap_points.append(
        {
            "address": address,
            "postcode": postcode,
            "sap_points": solid_recommendations["sap_points"]
        }
    )

solid_wall_sap_points = pd.DataFrame(solid_wall_sap_points)
avg_points = solid_wall_sap_points["sap_points"].median()

asset_list = asset_list.merge(solid_wall_sap_points, how="left", on=["address", "postcode"])
asset_list["sap_points"] = asset_list["sap_points"].fillna(avg_points)
asset_list["post_works_sap"] = asset_list["epc_sap_score_on_register"] + asset_list["sap_points"]
asset_list["post_works_epc"] = asset_list["post_works_sap"].apply(lambda x: sap_to_epc(x))
asset_list["starting_half_band"] = asset_list["epc_sap_score_on_register"].apply(lambda x: Funding.get_sap_band(x))
asset_list["ending_half_band"] = asset_list["post_works_sap"].apply(lambda x: Funding.get_sap_band(x))
asset_list["floor_area_band"] = asset_list["epc_total_floor_area"].apply(lambda x: Funding.get_floor_area_band(x))

asset_list["funding_scheme"] = np.where(
    (
        (asset_list["post_works_epc"] == asset_list["epc_rating_on_register"])
    ),
    "GBIS",
    "ECO4"
)

# Merge on the ABS matrix
asset_list = asset_list.merge(
    abs_matrix, how="left", left_on=["starting_half_band", "ending_half_band", "floor_area_band"],
    right_on=['Starting Band', 'Finishing Band', 'Floor Area Segment', ]
)
asset_list = asset_list.drop(columns=['Starting Band', 'Finishing Band', 'Floor Area Segment'])

# store for backup
# asset_list.to_csv(
#     "/Users/khalimconn-kowlessar/Documents/hestia/Customers/ACIS/Solid Wall Properties -
#     Standardised_2_with_funding.csv",
#     index=False
# )

# For GBIS, we use the PPS
# Almost all properties are gas

# Using IWI solid 1.7 -> 0.3 rates
pps_matrix = pps_matrix[
    pps_matrix["Measure_Type"].isin(["IWI_solid_1.7_0.3"])
]

# Merge on
asset_list = asset_list.merge(
    pps_matrix[['Starting Band', 'Total Floor Area Band', 'Cost Savings']].rename(
        columns={
            "Cost Savings": "partial_project_score",
            "Starting Band": "starting_half_band",
            "Total Floor Area Band": "floor_area_band"
        }
    ),
    how="left",
    on=["starting_half_band", "floor_area_band"],
)
asset_list["partial_project_score"] = np.where(
    asset_list["starting_half_band"].isin(["Low_C", "High_C"]),
    None,
    asset_list["partial_project_score"]
)

asset_list["funding_abs"] = np.where(
    asset_list["funding_scheme"] == "GBIS",
    asset_list["partial_project_score"],
    asset_list["Cost Savings"]
)

asset_list["heat_loss_area"] = asset_list.apply(
    lambda x: estimate_external_wall_area(
        num_floors=x["attribute_est_number_floors"],
        floor_height=(
            float(x["epc_floor_height"]) if
            not pd.isnull(x["epc_floor_height"]) else 2.5
        ),
        perimeter=x["attribute_est_perimter"],
        built_form=x["epc_archetype"]
    ),
    axis=1
)

filename = "/Users/khalimconn-kowlessar/Documents/hestia/Customers/ACIS/20250624 ACIS solid wall - standardised.xlsx"

with pd.ExcelWriter(filename) as writer:
    asset_list.to_excel(writer, sheet_name="Standardised Asset List", index=False)
