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 = 122


def app():
    asset_list = [
        {
            "address": "12 Church Lane", "postcode": "CB23 8AF", "uprn": 100090136018,
            "property_type": "House", "built-form": "Semi-Detached"
        },
        {
            "address": "21 High Street", "postcode": "CB23 8AB", "uprn": 100090144815
        },
        {
            "address": "22 High Street", "postcode": "CB23 8AB", "uprn": 100090144816
        },
        {
            "address": "5 Bunkers Hill", "postcode": "CB3 0LY", "uprn": 10008078615
        },
        {
            "address": "6 Bunkers Hill", "postcode": "CB3 0LY", "uprn": 10008078616
        },
        {
            "address": "7 Bunkers Hill", "postcode": "CB3 0LY", "uprn": 10008078617
        },
        {
            "address": "32 George Nuttall Close", "postcode": "CB4 1YE", "uprn": 200004200075
        },
        {
            "address": "33 George Nuttall Close", "postcode": "CB4 1YE", "uprn": 200004200076
        },
        {
            "address": "35 George Nuttall Close", "postcode": "CB4 1YE", "uprn": 200004200078
        },
        {
            "address": "36 George Nuttall Close", "postcode": "CB4 1YE", "uprn": 200004200079
        }
    ]
    asset_list = pd.DataFrame(asset_list)

    valuations_data = [
        {'uprn': 100090136018, "valuation": 586_000},
        {'uprn': 100090144815, "valuation": 446_000},
        {'uprn': 100090144816, "valuation": 448_000},
        {'uprn': 10008078615, "valuation": 763_000},
        {'uprn': 10008078616, "valuation": 616_000},
        {'uprn': 10008078617, "valuation": 593_000},
        {'uprn': 200004200075, "valuation": 450_000},
        {'uprn': 200004200076, "valuation": 457_000},
        {'uprn': 200004200078, "valuation": 304_000},
        {'uprn': 200004200079, "valuation": 313_000}
    ]

    # Pull the additional data
    extracted_data = []
    for _, home in tqdm(asset_list.iterrows(), total=len(asset_list)):
        add1 = home["address"]
        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)
        if epc_searcher.newest_epc is None:
            continue

        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,
            }
        )

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

    filename = f"{USER_ID}/{PORTFOLIO_ID}/asset_list.csv"
    save_csv_to_s3(
        dataframe=pd.DataFrame(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=pd.DataFrame(valuations_data),
        bucket_name="retrofit-plan-inputs-dev",
        file_name=valuations_filename
    )

    body = {
        "portfolio_id": str(PORTFOLIO_ID),
        "housing_type": "Private",
        "goal": "Increasing EPC",
        "goal_value": "B",
        "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": []
    }
    print(body)
