import pandas as pd
from utils.s3 import save_csv_to_s3
from datetime import datetime
from etl.non_intrusive_surveys.upload.UploadNonIntrusives import UploadNonIntrusives

PORTFOLIO_ID = 82
USER_ID = 8

already_installed = [
    {
        'address': 'Flat 3 2 Linacre Lane',
        'postcode': 'L20 5AH',
        "already_installed": ["windows_glazing"]
    }
]


def app():
    """
    This script handles the creation of the portfolio for the non-intrusive surveys
    :return:
    """

    # In the future, we can just use the ordnance survey api
    uprn_lookup = [
        {'House Number': 79,
         'Address Line 1': 'Clare Road',
         'Address Line 2': 'Liverpool',
         'Postcode': 'L20 9LZ',
         'uprn': 41018850},
        {'House Number': 'Flat 1',
         'Address Line 1': '2 Linacre Lane',
         'Address Line 2': 'Liverpool',
         'Postcode': 'L20 5AH',
         'uprn': 41052320},
        {'House Number': 'Flat 2',
         'Address Line 1': '2 Linacre Lane',
         'Address Line 2': 'Liverpool',
         'Postcode': 'L20 5AH',
         'uprn': 41052321},
        {'House Number': 'Flat 3',
         'Address Line 1': '2 Linacre Lane',
         'Address Line 2': 'Liverpool',
         'Postcode': 'L20 5AH',
         'uprn': 41052322},
        {'House Number': 'Flat 4',
         'Address Line 1': '2 Linacre Lane',
         'Address Line 2': 'Liverpool',
         'Postcode': 'L20 5AH',
         'uprn': 41222759},
        {'House Number': 'Flat 1',
         'Address Line 1': '4 Linacre Lane',
         'Address Line 2': 'Liverpool',
         'Postcode': 'L20 5AH',
         'uprn': 41222760},
        {'House Number': 'Flat 2 (NO ACCESS)',
         'Address Line 1': '4 Linacre Lane',
         'Address Line 2': 'Liverpool',
         'Postcode': 'L20 5AH',
         'uprn': 41222761},
        {'House Number': 'Flat 3',
         'Address Line 1': '4 Linacre Lane',
         'Address Line 2': 'Liverpool',
         'Postcode': 'L20 5AH',
         'uprn': 41212534},
        {'House Number': 'Flat 1 (NO ACCESS)',
         'Address Line 1': '29 Bedford Road',
         'Address Line 2': 'Liverpool',
         'Postcode': 'L4 5PS',
         'uprn': 38237316},
        {'House Number': 'Flat 2 (NO ACCESS)',
         'Address Line 1': '29 Bedford Road',
         'Address Line 2': 'Liverpool',
         'Postcode': 'L4 5PS',
         'uprn': 38237317}
    ]

    non_intrusive_s3_filename = (
        "customers/Vander Elliot/Non-intrusive survey template V2 - Amazon Management Services.xlsx"
    )

    non_intrusive = UploadNonIntrusives(
        s3_template_location=non_intrusive_s3_filename,
        s3_bucket="retrofit-datalake-dev",
        uprn_lookup=uprn_lookup,
        survey_date=datetime.strptime('2024-06-21', '%Y-%m-%d')
    )
    non_intrusive.upload()

    # We can create the asset list from the uprn lookup
    asset_list = [
        {
            "uprn": x["uprn"],
            "address": f"{x['House Number']} {x['Address Line 1']}",
            "postcode": x["Postcode"]
        }
        for x in uprn_lookup
    ]

    asset_list = pd.DataFrame(asset_list)

    # 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 the asset list in s3
    filename = f"{USER_ID}/{PORTFOLIO_ID}/non_intrusives.csv"
    save_csv_to_s3(
        dataframe=asset_list,
        bucket_name="retrofit-plan-inputs-dev",
        file_name=filename
    )

    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": "",
        "non_invasive_recommendations_file_path": "",
        "exclusions": ["wall_insulation", "air_source_heat_pump"],
        "budget": None,
    }
    print(body)
