import pandas as pd

from utils.s3 import save_csv_to_s3

PORTFOLIO_ID = 83
SECOND_PORTFOLIO_ID = 84
USER_ID = 8


def app():
    # TODO: We can insert a variable, indicating the they own all of the units in the building
    asset_list = [
        {
            "address": "Flat 1, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140644,
            "building_id": 1,
        },
        {
            "address": "Flat 2, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140645,
            "building_id": 1,
        },
        {
            "address": "Flat 3, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140646,
            "building_id": 1,
        },
        {
            "address": "Flat 4, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140647,
            "building_id": 1,
        },
        {
            "address": "Flat 5, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140648,
            "building_id": 1,
        },
        {
            "address": "Flat 6, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140649,
            "building_id": 1,
        }
    ]

    asset_list = pd.DataFrame(asset_list)

    # 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": "B",
        "trigger_file_path": filename,
        "already_installed_file_path": "",
        "patches_file_path": "",
        "non_invasive_recommendations_file_path": "",
        "budget": None,
        "exclusions": ["floor_insulation"]
    }
    print(body)

    # Get an example of flats with solar panels from epc data

    # import inspect
    # import pandas as pd
    # from tqdm import tqdm
    # from pathlib import Path
    #
    # src_file_path = inspect.getfile(lambda: None)
    #
    # EPC_DIRECTORY = Path(src_file_path).parent / "local_data" / "all-domestic-certificates"
    #
    # epc_directories = [entry for entry in EPC_DIRECTORY.iterdir() if entry.is_dir()]
    #
    # directory = epc_directories[1]
    # data = pd.read_csv(directory / "certificates.csv", low_memory=False)
    # # Get flats
    # data = data[data["PROPERTY_TYPE"].str.lower().str.contains("flat")]
    # data = data[~pd.isnull(data["UPRN"])]
    # data["UPRN"] = data["UPRN"].astype(int).astype(str)
    # data = data[pd.to_datetime(data["LODGEMENT_DATE"]) > "2020-01-01"]
    # flats_with_solar = data[data['PHOTO_SUPPLY'] > 0]
    #
    # print(flats_with_solar["UPRN"])
    #
    # flats_with_solar[["ADDRESS", "UPRN"]]
    #
    # # Good example:
    # # UPRN: 10013160824, Flat 39, The Meadow, 30 Busk Meadow S5 7JH (care home with 39 flats, have solar panels)
    # #
    # # Mostly, For a mid-floor flat, the property doesn't show as having solar panels through the photo_supply variable
    # # But actually for UPRN: 10013245713, Apartment 4, Orchard House, Gill Lane PR4 5QN, this has a dwelling above
    # # but the photo_supply variable is 20
    #
    # # Small flat consisting of 2 units
    # # UPRN: 42172953, FLAT 2, 276 CLAUGHTON ROAD, BIRKENHEAD CH41 4DX
    #
    # # Flat containing 5 units
    # # UPRN: 10013247127 Flat 1, Old Church House PR4 5GE
    # # UPRN: 10013247130 Flat 4, Old Church House PR4 5GE
    #
    # # Flat containing multiple units:
    # # UPRNS: 10013245710, 10013245716, 10013245711, 10013245717, 10013245714, 10013245715, 10013245712, 10013245713
    #
    # # Look for flats with air source heat pumps!
    # flats_with_asps = data[data["MAINHEAT_DESCRIPTION"].str.lower().str.contains("air source heat pump")]
    # print(flats_with_asps[["UPRN", "ADDRESS"]])


def app_epc_b():
    # TODO: We can insert a variable, indicating the they own all of the units in the building
    asset_list = [
        {
            "address": "Flat 1, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140644,
            "building_id": 1,
        },
        {
            "address": "Flat 2, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140645,
            "building_id": 1,
        },
        {
            "address": "Flat 3, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140646,
            "building_id": 1,
        },
        {
            "address": "Flat 4, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140647,
            "building_id": 1,
        },
        {
            "address": "Flat 5, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140648,
            "building_id": 1,
        },
        {
            "address": "Flat 6, Fenton Court",
            "postcode": "N2 8DS",
            "uprn": 200140649,
            "building_id": 1,
        }
    ]

    non_invasive_recommendations = [
        {
            "address": "Flat 1, Fenton Court",
            "postcode": "N2 8DS",
            'recommendations': [
                'cavity_extract_and_refill',
                # 'air_source_heat_pump'
            ]
        },
        {
            "address": "Flat 2, Fenton Court",
            "postcode": "N2 8DS",
            'recommendations': [
                'cavity_extract_and_refill',
                # 'air_source_heat_pump'
            ]
        },
        {
            "address": "Flat 3, Fenton Court",
            "postcode": "N2 8DS",
            'recommendations': [
                'cavity_extract_and_refill',
                # 'air_source_heat_pump'
            ]
        },
        {
            "address": "Flat 4, Fenton Court",
            "postcode": "N2 8DS",
            'recommendations': [
                'cavity_extract_and_refill',
                # 'air_source_heat_pump'
            ]
        },
        {
            "address": "Flat 5, Fenton Court",
            "postcode": "N2 8DS",
            'recommendations': [
                'cavity_extract_and_refill',
                'loft_insulation',
                # 'air_source_heat_pump'
            ]
        },
        {
            "address": "Flat 6, Fenton Court",
            "postcode": "N2 8DS",
            'recommendations': [
                'cavity_extract_and_refill',
                'loft_insulation',
                # 'air_source_heat_pump'
            ]
        },
    ]

    asset_list = pd.DataFrame(asset_list)

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

    # Store non-invasive recommendations in S3
    non_invasive_recommendations_filename = f"{USER_ID}/{SECOND_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
    )

    body = {
        "portfolio_id": str(SECOND_PORTFOLIO_ID),
        "housing_type": "Private",
        "goal": "Increase EPC",
        "goal_value": "B",
        "trigger_file_path": filename,
        "already_installed_file_path": "",
        "patches_file_path": "",
        "non_invasive_recommendations_file_path": non_invasive_recommendations_filename,
        "budget": None,
        "exclusions": ["floor_insulation"]
    }
    print(body)

    # Get an example of flats with solar panels from epc data

    # import inspect
    # import pandas as pd
    # from tqdm import tqdm
    # from pathlib import Path
    #
    # src_file_path = inspect.getfile(lambda: None)
    #
    # EPC_DIRECTORY = Path(src_file_path).parent / "local_data" / "all-domestic-certificates"
    #
    # epc_directories = [entry for entry in EPC_DIRECTORY.iterdir() if entry.is_dir()]
    #
    # directory = epc_directories[1]
    # data = pd.read_csv(directory / "certificates.csv", low_memory=False)
    # # Get flats
    # data = data[data["PROPERTY_TYPE"].str.lower().str.contains("flat")]
    # data = data[~pd.isnull(data["UPRN"])]
    # data["UPRN"] = data["UPRN"].astype(int).astype(str)
    # data = data[pd.to_datetime(data["LODGEMENT_DATE"]) > "2020-01-01"]
    # flats_with_solar = data[data['PHOTO_SUPPLY'] > 0]
    #
    # print(flats_with_solar["UPRN"])
    #
    # flats_with_solar[["ADDRESS", "UPRN"]]
    #
    # # Good example:
    # # UPRN: 10013160824, Flat 39, The Meadow, 30 Busk Meadow S5 7JH (care home with 39 flats, have solar panels)
    # #
    # # Mostly, For a mid-floor flat, the property doesn't show as having solar panels through the photo_supply variable
    # # But actually for UPRN: 10013245713, Apartment 4, Orchard House, Gill Lane PR4 5QN, this has a dwelling above
    # # but the photo_supply variable is 20
    #
    # # Small flat consisting of 2 units
    # # UPRN: 42172953, FLAT 2, 276 CLAUGHTON ROAD, BIRKENHEAD CH41 4DX
    #
    # # Flat containing 5 units
    # # UPRN: 10013247127 Flat 1, Old Church House PR4 5GE
    # # UPRN: 10013247130 Flat 4, Old Church House PR4 5GE
    #
    # # Flat containing multiple units:
    # # UPRNS: 10013245710, 10013245716, 10013245711, 10013245717, 10013245714, 10013245715, 10013245712, 10013245713
    #
    # # Look for flats with air source heat pumps!
    # flats_with_asps = data[data["MAINHEAT_DESCRIPTION"].str.lower().str.contains("air source heat pump")]
    # print(flats_with_asps[["UPRN", "ADDRESS"]])
