import os
import pandas as pd
from dotenv import load_dotenv
from utils.s3 import save_csv_to_s3
from etl.find_my_epc.AssetListEpcData import AssetListEpcData

PORTFOLIO_ID = 235
USER_ID = 8

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


def app():
    """
    This application is used to initialise and run remote assessments
    :return:
    """

    # asset_list = pd.read_excel(
    #     "/Users/khalimconn-kowlessar/Downloads/Energy Information MASTER June 2025 - Standardised.xlsx",
    #     sheet_name="Solar Properties",
    # )
    # asset_list = asset_list[~asset_list["estimated"]]
    # asset_list["domna_address_1"] = asset_list["domna_address_1"].astype(str)
    # asset_list = asset_list[["domna_address_1", "domna_postcode", "epc_os_uprn"]].rename(
    #     columns={"domna_address_1": "address", "domna_postcode": "postcode", "epc_os_uprn": "uprn"}
    # )

    asset_list = [
        {
            "address": "9 Reeds Place",
            "postcode": "PO12 3HR",
            "uprn": 37017508
        },
        {
            "address": "7 Crawley Road",
            "postcode": "N22 6AN",
            "uprn": 100021169757
        },
        {
            "address": "20 Main Street",
            "postcode": "NG32 1SE",
            "uprn": 200002698370
        },
        {
            "address": "19 Wolley Avenue",
            "postcode": "LS12 5DX",
            "uprn": 72234517
        },
        {
            "address": "45 Bolton Lane, Hose",
            "postcode": "LE14 4JE",
            "uprn": 100030535501
        }
    ]

    asset_list = pd.DataFrame(asset_list)

    # Store the asset list in s3
    filename = f"{USER_ID}/{PORTFOLIO_ID}/asset_list.csv"
    save_csv_to_s3(
        dataframe=asset_list,
        bucket_name="retrofit-plan-inputs-dev",
        file_name=filename
    )

    # Pull the non-invasive recommendations automatically
    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()
    asset_list_epc_client.get_patch()

    # Store 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(asset_list_epc_client.non_invasive_recommendations),
        bucket_name="retrofit-plan-inputs-dev",
        file_name=non_invasive_recommendations_filename
    )

    # Store patches in S3
    patches_filename = ""
    if asset_list_epc_client.patches:
        patches_filename = f"{USER_ID}/{PORTFOLIO_ID}/patches.csv"
        save_csv_to_s3(
            dataframe=pd.DataFrame(asset_list_epc_client.patches),
            bucket_name="retrofit-plan-inputs-dev",
            file_name=patches_filename
        )

    valuation_data = [
        {
            "valuation": 201000,
            "uprn": 37017508,
        },
        {
            "valuation": 810000,
            "uprn": 100021169757,
        },
        {
            "valuation": 228_000,
            "uprn": 72234517
        },
        {
            "valuation": 236_000,
            "uprn": 100030535501
        },
        {
            "valuation": 509000,
            "uprn": 200002698370
        },
    ]
    # Store valuation data to s3
    valuation_filename = f"{USER_ID}/{PORTFOLIO_ID}/valuation.csv"
    save_csv_to_s3(
        dataframe=pd.DataFrame(valuation_data),
        bucket_name="retrofit-plan-inputs-dev",
        file_name=valuation_filename
    )

    body1 = {
        "portfolio_id": str(PORTFOLIO_ID),
        "housing_type": "Social",
        "goal": "Increasing EPC",
        "goal_value": "B",
        "trigger_file_path": filename,
        "already_installed_file_path": "",
        "patches_file_path": "",
        "non_invasive_recommendations_file_path": "",
        "valuation_file_path": valuation_filename,
        "scenario_name": "EPC B",
        "multi_plan": True,
        "budget": None,
        "ashp_cop": 3.5,
        "event_type": "remote_assessment",
        "default_u_values": True,

    }
    print(body1)

    body2 = {
        "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": "",
        "valuation_file_path": valuation_filename,
        "scenario_name": "EPC C",
        "multi_plan": True,
        "budget": None,
        "ashp_cop": 3.5,
        "event_type": "remote_assessment",
        "default_u_values": True,

    }
    print(body2)
