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

council_tax_bands = [
    {'address': '8 Corporation Road', 'postcode': 'DY2 7PX', 'band': 'A'},
    {'address': '21 Wells Road', 'postcode': 'DY5 3TB', 'band': 'A'},
    {'address': '27 Milton Road', 'postcode': 'WV14 8HZ', 'band': 'A'},
    {'address': '195 Ashenhurst Road', 'postcode': 'DY1 2JB', 'band': 'A'},
    {'address': '53 Bromley', 'postcode': 'DY5 4PJ', 'band': 'A'},
    {'address': '91 Osprey Drive', 'postcode': 'DY1 2JS', 'band': 'B'},
    {'address': '47 Fairfield Road', 'postcode': 'DY8 5UJ', 'band': 'B'},
    {'address': '150 Huntingtree Road', 'postcode': 'B63 4HP', 'band': 'C'},
    {'address': '6 Beech Road', 'postcode': 'DY1 4BP', 'band': 'A'},
    {'address': '5 Oaklands', 'postcode': 'B62 0JA', 'band': 'A'},
]
council_tax_bands = pd.DataFrame(council_tax_bands)

# This is information we need to override on the EPC itself, for instance if a new survey has been conducted and
# that has not reached the API
# For 53 Bromley, the non-invasives found the walls to be partially filled
patches = [
    {
        'address': '6 Beech Road', 'postcode': 'DY1 4BP',
        'walls-description': 'Cavity wall, filled cavity',
        'walls-energy-eff': 'Good',
        'roof-description': 'Pitched, 12 mm loft insulation',
        'roof-energy-eff': 'Very Poor',
        'windows-description': 'Fully double glazed',
        'windows-energy-eff': 'Good',
        'mainheat-description': 'Room heaters, electric',
        'mainheat-energy-eff': 'Very Poor',
        'mainheatcont-description': 'Appliance thermostats',
        'mainheatc-energy-eff': 'Good',
        'lighting-description': 'Low energy lighting in 25% of fixed outlets',
        'lighting-energy-eff': 'Good',
        'floor-description': 'Solid, no insulation (assumed)',
        'secondheat-description': 'None',
        'current-energy-efficiency': '32',
        'energy-consumption-current': '491',
        'co2-emissions-current': '5.0',
        'potential-energy-efficiency': '87'
    },
    {
        'address': '53 Bromley', 'postcode': 'DY5 4PJ',
        'walls-description': 'Cavity wall, partial insulation (assumed)',
    },
]

# 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': '5 Oaklands',
        'postcode': 'B62 0JA',
        "already_installed": ["windows_glazing"]
    }
]

non_invasive_recommendations = [
    {'address': '8 Corporation Road', 'postcode': 'DY2 7PX', 'recommendations': []},
    {'address': '21 Wells Road', 'postcode': 'DY5 3TB', 'recommendations': ['cavity_extract_and_refill']},
    {'address': '27 Milton Road', 'postcode': 'WV14 8HZ', 'recommendations': ['cavity_extract_and_refill']},
    {'address': '195 Ashenhurst Road', 'postcode': 'DY1 2JB', 'recommendations': ['cavity_extract_and_refill']},
    {'address': '53 Bromley', 'postcode': 'DY5 4PJ', 'recommendations': ['cavity_surveyed_as_filled_is_partial']},
    {'address': '91 Osprey Drive', 'postcode': 'DY1 2JS', 'recommendations': ['cavity_extract_and_refill']},
    {'address': '47 Fairfield Road', 'postcode': 'DY8 5UJ', 'recommendations': ['cavity_extract_and_refill']},
    {'address': '150 Huntingtree Road', 'postcode': 'B63 4HP', 'recommendations': ['cavity_extract_and_refill']},
    {'address': '6 Beech Road', 'postcode': 'DY1 4BP', 'recommendations': []},
    {'address': '5 Oaklands', 'postcode': 'B62 0JA', 'recommendations': ['cavity_extract_and_refill']},
]


def app():
    raw_asset_list = read_excel_from_s3(
        bucket_name="retrofit-datalake-dev",
        file_key="customers/Immo/IMMO Sample Assets_Dudley.xlsx",
        header_row=0
    )
    raw_asset_list = raw_asset_list.drop(columns=["Unnamed: 0"])
    # 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()

    asset_list = raw_asset_list.merge(council_tax_bands, how="left", on=["address", "postcode"])

    # We're provided with number of bathrooms and number of bedrooms.
    asset_list = asset_list.rename(
        columns={
            "No. of Beds": "n_bedrooms",
            "No. of WC's": "n_bathrooms"
        }
    )

    # 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)
