import os

import pandas as pd
from dotenv import load_dotenv

from etl.spatial.OpenUprnClient import OpenUprnClient
from etl.route_march_data_pull.app import get_data

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

addresses = [
    {"address": "3 Willis Road", "postcode": "CB1 2AQ"},
    {"address": "22 Catharine Street", "postcode": "CB1 3AW"},
    {"address": "332 Mill Road", "postcode": "CB1 3NN"},
    {"address": "330 Mill Road", "postcode": "CB1 3NN"},
    {"address": "328 Mill Road", "postcode": "CB1 3NN"},
    {"address": "71 Mill Road", "postcode": "CB1 2AS"},
    {"address": "78 Argyle Street", "postcode": "CB1 3LZ"},
    {"address": "9 Graham Road", "postcode": "CB4 2ZE"},
    {"address": "217 Mill Road", "postcode": "CB1 3BE"},
    {"address": "374 Mill Road", "postcode": "CB1 3NN"},
    {"address": "174 Thoday Street", "postcode": "CB1 3AX"},
    {"address": "37 Abbey Road", "postcode": "CB5 8HH"},
    {"address": "18 Upper Gwydir Street", "postcode": "CB1 2LR"},
    {"address": "21 Fulbourn Road Fulbourn", "postcode": "CB1 9JL"},
    {"address": "108 Argyle Street", "postcode": "CB1 3LS"},
    {"address": "115 Victoria Road", "postcode": "CB4 3BS"},
    {"address": "55 Ross Street", "postcode": "CB1 3BP"},
    {"address": "16 Kingston Street", "postcode": "CB1 2NU"},
    {"address": "13 Thoday Street", "postcode": "CB1 3AS"},
    {"address": "103 York Street", "postcode": "CB1 2PZ"},
]

asset_list = pd.DataFrame(addresses)
asset_list["row_id"] = asset_list.index

epc_data, _, _ = get_data(
    asset_list=asset_list, fulladdress_column="address", postcode_column="postcode", address1_column="address",
    manual_uprn_map={}, epc_api_only=True
)

epc_df = pd.DataFrame(epc_data)
epc_df.shape

asset_list = asset_list.merge(
    epc_df, how="left", on="row_id"
)

asset_list = asset_list.rename(columns={"address_x": "Address", "postcode_x": "Postcode"})
asset_list["uprn"] = asset_list["uprn"].astype(str)

spatial_data = OpenUprnClient.get_spatial_data([x["uprn"] for x in epc_data], bucket_name="retrofit-data-dev")
spatial_data["UPRN"] = spatial_data["UPRN"].astype(str)

asset_list = asset_list.merge(
    spatial_data, how="left", left_on="uprn", right_on="UPRN"
)

asset_list.to_csv("/Users/khalimconn-kowlessar/Documents/hestia/Customers/Panacap/Acquisitions EPC Data.csv",
                  index=False)
