import pandas as pd
from utils.s3 import read_excel_from_s3
from utils.s3 import save_csv_to_s3

USER_ID = 8
PORTFOLIO_ID = 72

# For
patches = [
    {
        'address': '116 Parkes Hall Road',
        'postcode': 'DY1 3RJ',
        'uprn': '90046817',
        'walls-description': 'Cavity wall, filled cavity',
        'walls-energy-eff': 'Average',
        'roof-description': 'Pitched, 270 mm loft insulation',
        'roof-energy-eff': 'Good',
        'windows-description': 'Fully double glazed',
        'windows-energy-eff': 'Good',
        'mainheat-description': 'Boiler and radiators, mains gas',
        'mainheat-energy-eff': 'Good',
        'mainheatcont-description': 'Programmer, room thermostat and TRVs',
        'mainheatc-energy-eff': 'Good',
        'lighting-description': 'Low energy lighting in 27% of fixed outlets',
        'lighting-energy-eff': 'Average',
        'floor-description': 'Solid, no insulation (assumed)',
        'secondheat-description': 'None',
        'current-energy-efficiency': '73',
        'current-energy-rating': 'C',
        'energy-consumption-current': '184',
        'co2-emissions-current': '2.4',
        'potential-energy-efficiency': '88',
        'total-floor-area': '73',
        'construction-age-band': 'England and Wales: 1930-1949',
        'property-type': 'House',
        'built-form': 'Mid-Terrace',
    }
]

# This is information that is found as a result of the non-invasives, that mean that certain measures
# have been installed already. To reflect this in the front end, it is included in the recommendation, however
# the cost is removed and instead, a message is presented saying that the measure is already installed.
already_installed = [
    {
        'address': '28 Sangwin Road', 'postcode': 'WV14 9EQ', "already_installed": ["loft_insulation"]
    },
    {
        'address': '51 Hillwood Road', 'postcode': 'B62 8NQ', "already_installed": ["loft_insulation"]
    },
    {
        'address': '47 Watsons Close', 'postcode': 'DY2 7HL', "already_installed": ["loft_insulation"]
    },
    {
        'address': '44 Hatfield Road',
        'postcode': 'DY9 7LW',
        "already_installed": ["loft_insulation", "cavity_wall_insulation"]
    }
]

non_invasive_recommendations = []


def app():
    raw_asset_list = read_excel_from_s3(
        bucket_name="retrofit-datalake-dev",
        file_key="customers/Immo/Dudley Asset List - Hestia - pilot2.xlsx",
        header_row=0
    )

    raw_asset_list = raw_asset_list[raw_asset_list["in_pilot"]].copy()

    # Extract address and postcode
    raw_asset_list["address"] = raw_asset_list["Full Address"].str.split(",").str[0]
    raw_asset_list["postcode"] = raw_asset_list["Full Address"].str.split(",").str[-1].str.strip()

    # We're provided with number of bathrooms and number of bedrooms.
    # THe UPRNs are not the official ones
    asset_list = raw_asset_list.rename(
        columns={
            "No. of Beds": "n_bedrooms",
            "No. of WC's": "n_bathrooms",
            'Property Type': 'property_type',
            'Architype': 'built_form'
        }
    )

    # Remap the values
    asset_list["built_form"] = asset_list["built_form"].map({
        "SEMI DETACHED": "Semi-Detached",
        "MID TERRACE": "Mid-Terrace",
        "END TERRACE": "End-Terrace",
    })

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

    # Store overrides in s3
    already_installed_filename = f"{USER_ID}/{PORTFOLIO_ID}/already_installed.json"
    save_csv_to_s3(
        dataframe=pd.DataFrame(already_installed),
        bucket_name="retrofit-plan-inputs-dev",
        file_name=already_installed_filename
    )

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

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

    # EPC C portoflio
    body = {
        "portfolio_id": str(PORTFOLIO_ID),
        "housing_type": "Private",
        "goal": "Increase EPC",
        "goal_value": "C",
        "trigger_file_path": filename,
        "already_installed_file_path": already_installed_filename,
        "patches_file_path": patches_filename,
        "non_invasive_recommendations_file_path": non_invasive_recommendations_filename,
        "budget": None,
    }
    print(body)

    # EPC B portoflio
    body = {
        "portfolio_id": str(PORTFOLIO_ID + 1),
        "housing_type": "Private",
        "goal": "Increase EPC",
        "goal_value": "B",
        "trigger_file_path": filename,
        "already_installed_file_path": already_installed_filename,
        "patches_file_path": patches_filename,
        "non_invasive_recommendations_file_path": non_invasive_recommendations_filename,
        "budget": None,
    }
    print(body)
