"""
Rough script to get the EPC data for Benyon
"""

import pandas as pd
import os
from dotenv import load_dotenv
from backend.SearchEpc import SearchEpc
from asset_list.utils import get_data

load_dotenv(dotenv_path="backend/.env")
EPC_AUTH_TOKEN = os.getenv("EPC_AUTH_TOKEN")

asset_list = pd.read_excel(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/Benyon Estate/List of All Properties ecl Grd Rents in "
    "Alphabetical Order.xlsx",
    header=1
)
asset_list.columns = ["tennancy", "landlord_id", "landlord_address"]
# Get postcode as the last 2 parts of the address, split on space
asset_list["postcode"] = asset_list["landlord_address"].apply(lambda x: x.split(" ")[-2] + " " + x.split(" ")[-1])

asset_list["house_no"] = asset_list.apply(
    lambda x: SearchEpc.get_house_number(address=x["landlord_address"], postcode=x["postcode"]), axis=1
)

epc_data, errors, no_epc = get_data(
    df=asset_list,
    manual_uprn_map={},
    epc_auth_token=EPC_AUTH_TOKEN,
    uprn_column=None,
    fulladdress_column="landlord_address",
    address1_column="house_no",
    postcode_column="postcode",
    property_type_column=None,
    built_form_column=None,
    epc_api_only=True,
    row_id_name="landlord_id",
)

df = asset_list[asset_list["landlord_id"].isin(no_epc)]
epc_df = pd.DataFrame(epc_data)
epc_df["current-energy-rating"].value_counts()
epc_df["property-type"].value_counts()
epc_df["walls-description"].value_counts(normalize=True)

asset_list = asset_list.merge(
    epc_df[
        [
            "landlord_id", "current-energy-rating", "property-type", "total-floor-area", "roof-description",
            "walls-description", "co2-emissions-current"
        ]
    ],
    how="left",
    left_on="landlord_id",
    right_on="landlord_id"
)
asset_list.to_csv(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/Benyon Estate/asset_list.csv", index=False
)

asset_list_big = asset_list.merge(
    epc_df,
    how="left",
    left_on="landlord_id",
    right_on="landlord_id"
)
asset_list_big.to_csv(
    "/Users/khalimconn-kowlessar/Documents/hestia/Customers/Benyon Estate/asset_list_full_data.csv",
    index=False
)
