#
import pandas as pd

asset_list = pd.read_excel(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/MHS/New programme/12052025 MHS Standardised Asset List - "
    "programme.xlsx",
    sheet_name="Standardised Asset List"
)

new_cavity_programme = pd.read_excel(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/MHS/New programme/12052025 MHS Standardised Asset List - "
    "programme.xlsx",
    sheet_name="New Cavity Programme"
)

new_cavity_pilot = pd.read_excel(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/MHS/New programme/12052025 MHS Standardised Asset List - "
    "programme.xlsx",
    sheet_name="Empty Cavity Pilot"
)

new_solar_programme = pd.read_excel(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/MHS/New programme/12052025 MHS Standardised Asset List - "
    "programme.xlsx",
    sheet_name="New Solar Programme"
)

in_fill_properties_houses = pd.read_excel(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/MHS/New programme/Domna updated programme list May 2025 ("
    "1).xlsx",
    sheet_name="Houses and Bungalows"
)
in_fill_properties_flats = pd.read_excel(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/MHS/New programme/Domna updated programme list May 2025 ("
    "1).xlsx",
    sheet_name="Flats and Maistonettes"
)
# Q1) What are these properties? Do we have them on our list already?
# All of the houses are already in the asset list
in_fill_properties_houses["is_in_asset_list"] = in_fill_properties_houses["UPRN"].isin(
    asset_list["landlord_property_id"].values
)
# All of the flats are already in the asset list
in_fill_properties_flats["is_in_asset_list"] = in_fill_properties_flats["UPRN"].isin(
    asset_list["landlord_property_id"].values
)

# Q2) Which properties are excluded from the new programme?
in_fill_properties = pd.concat(
    [in_fill_properties_houses, in_fill_properties_flats], ignore_index=True, sort=False
)

# Merge on the data
in_fill_properties = in_fill_properties.merge(
    asset_list,
    left_on="UPRN",
    right_on="landlord_property_id",
    how="left"
)
# How many properties are in the new programme?

in_fill_properties["in_new_cavity_programme"] = in_fill_properties["UPRN"].isin(
    new_cavity_programme["landlord_property_id"].values
)
in_fill_properties["in_new_solar_programme"] = in_fill_properties["UPRN"].isin(
    new_solar_programme["landlord_property_id"].values
)
in_fill_properties["in_new_cavity_pilot"] = in_fill_properties["UPRN"].isin(
    new_cavity_pilot["landlord_property_id"].values
)
not_in_new_programme = in_fill_properties[
    (~in_fill_properties["in_new_cavity_programme"] & ~in_fill_properties["in_new_solar_programme"] & ~
    in_fill_properties["in_new_cavity_pilot"])
].copy()

# Why?
not_in_new_programme["cavity_reason"].value_counts()
not_in_new_programme["solar_reason"].value_counts()

not_identified_for_anything = not_in_new_programme[
    pd.isnull(not_in_new_programme["cavity_reason"]) &
    pd.isnull(not_in_new_programme["solar_reason"])
    ]

# Flag the potential re-inspections which is 994 properties though any extractions we need to consider the HA funding
# the extraction
not_in_new_programme["funded_extractions"] = not_in_new_programme["cavity_reason"].isin(
    [
        "Non-Intrusive Data Shows Cavity Extraction: SAP Rating 69-75",
        "Non-Intrusive Data Shows Cavity Extraction: SAP Rating 55-68",
        "Non-Intrusive Data Shows Cavity Extraction: SAP Rating 76 or more",
        "Non-Intrusive Data Shows Cavity Extraction: SAP Rating 54 or less",
        "EPC Shows Empty Cavity, inspections show non-cavity build: SAP Rating 76 or more",
        "EPC Shows Empty Cavity, inspections show non-cavity build: SAP Rating 54 or less",
        "EPC Shows Empty Cavity, inspections show retro drilled: SAP Rating 54 or less",
        "EPC Shows Empty Cavity, inspections show retro drilled: SAP Rating 76 or more",
    ]
)

not_in_new_programme["excluded"] = not_identified_for_anything["landlord_property_id"].isin(
    not_identified_for_anything["landlord_property_id"].values
)

not_in_new_programme[
    not_in_new_programme["funded_extractions"]
].to_csv(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/MHS/New programme/funded_extractions.csv",
    index=False
)

not_in_new_programme[
    not_in_new_programme["excluded"] == True
    ].to_csv(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/MHS/New programme/excluded_properties.csv",
    index=False
)
