import os
import time

from tqdm import tqdm
import pandas as pd
from dotenv import load_dotenv
from etl.find_my_epc.RetrieveFindMyEpc import RetrieveFindMyEpc
from backend.SearchEpc import SearchEpc
from utils.s3 import save_csv_to_s3

load_dotenv(dotenv_path="backend/.env")
EPC_AUTH_TOKEN = os.getenv("EPC_AUTH_TOKEN")
USER_ID = 8
PORTFOLIO_ID = 121


def app():
    """
    Prepares the inputs to produce the remote assessments for Cottons
    :return:
    """

    # Read in the asset list
    cottons_asset_list = pd.read_excel(
        "/Users/khalimconn-kowlessar/Documents/hestia/Customers/Cottons/Cottons Asset List EPC Data Pull with "
        "valuations.xlsx"
    )
    # A number are missing EPCs due to the space in the postcode
    # Breakdowns:
    # C    119
    # D     106
    # E     26
    # B      5
    #
    # Take the EPC D/E properties
    asset_list = cottons_asset_list[
        cottons_asset_list["EPC rating on register"].isin(["D", "E"])
    ]
    asset_list = asset_list.reset_index(drop=True)
    asset_list["row_id"] = asset_list.index
    asset_list["uprn"] = asset_list["uprn"].astype(int)

    extracted_data = []
    model_asset_list = []
    for _, home in tqdm(asset_list.iterrows(), total=len(asset_list)):
        add1 = home["address1"]
        pc = home["postcode"]
        # Retrieve the EPC data
        epc_searcher = SearchEpc(
            address1=add1,
            postcode=pc, uprn=home["uprn"], auth_token=EPC_AUTH_TOKEN, os_api_key=""
        )
        epc_searcher.find_property(skip_os=True)

        find_epc_searcher = RetrieveFindMyEpc(address=epc_searcher.newest_epc["address1"],
                                              postcode=epc_searcher.newest_epc["postcode"])
        find_epc_data = find_epc_searcher.retrieve_newest_find_my_epc_data()
        time.sleep(0.5)
        # We need uprn

        extracted_data.append(
            {
                "uprn": home["uprn"],
                **find_epc_data,
            }
        )

        model_asset_list.append(
            {
                "uprn": home["uprn"],
                "address": epc_searcher.newest_epc["address1"],
                "postcode": epc_searcher.newest_epc["postcode"],
            }
        )

    non_invasive_recommendations = [
        {
            "uprn": r["uprn"],
            "recommendations": r["recommendations"]
        } for r in extracted_data
    ]

    valuations_data = asset_list[["uprn", "Zoopla Valuation"]].copy().rename(columns={"Zoopla Valuation": "valuation"})
    valuations_data = valuations_data[~pd.isnull(valuations_data["valuation"])]

    filename = f"{USER_ID}/{PORTFOLIO_ID}/asset_list.csv"
    save_csv_to_s3(
        dataframe=pd.DataFrame(model_asset_list),
        bucket_name="retrofit-plan-inputs-dev",
        file_name=filename
    )

    # Store the non-invasive recommendations in s3
    non_invasive_recommendations_filename = f"{USER_ID}/{PORTFOLIO_ID}/non_invasive_recommendations.csv"
    save_csv_to_s3(
        dataframe=pd.DataFrame(non_invasive_recommendations),
        bucket_name="retrofit-plan-inputs-dev",
        file_name=non_invasive_recommendations_filename
    )

    # Store the valuations data in s3
    valuations_filename = f"{USER_ID}/{PORTFOLIO_ID}/valuations.csv"
    save_csv_to_s3(
        dataframe=valuations_data,
        bucket_name="retrofit-plan-inputs-dev",
        file_name=valuations_filename
    )

    body = {
        "portfolio_id": str(PORTFOLIO_ID),
        "housing_type": "Social",
        "goal": "Increasing EPC",
        "goal_value": "C",
        "trigger_file_path": filename,
        "already_installed_file_path": "",
        "patches_file_path": "",
        "non_invasive_recommendations_file_path": non_invasive_recommendations_filename,
        "valuation_file_path": valuations_filename,
        "scenario_name": "Wave 3 Packages",
        "multi_plan": True,
        "budget": None,
        "exclusions": ['air_source_heat_pump', 'boiler_upgrade', 'floor_insulation']
    }
    print(body)
