import pandas as pd
import numpy as np
import time
from tqdm import tqdm
from etl.bill_savings.data_collection import retrieve_find_my_epc_data, calculate_expiry_date


def main():
    """
    This script handles pulling the surveyor names and acreditation details for Surveyors who have completed
    the newest EPC for AIHA's properties
    """

    epc_data = pd.read_csv("/Users/khalimconn-kowlessar/Documents/hestia/Customers/AIHA/epc_data.csv")
    epc_data = epc_data[["uprn", "address", "address1", "postcode", "lodgement-date"]]

    epc_collected_data = []
    for _, unit in tqdm(epc_data.iterrows(), total=len(epc_data)):
        time.sleep(np.random.uniform(0.2, 1.5))
        uprn = int(unit["uprn"])
        address = unit["address1"]
        postcode = unit["postcode"]
        expected_expiry_date = calculate_expiry_date(unit["lodgement-date"])

        response = retrieve_find_my_epc_data(
            uprn=uprn,
            postcode=postcode,
            address=address,
            expected_expiry_date=expected_expiry_date
        )
        if response is None:
            raise Exception("fix me")
        epc_collected_data.append(response)

    epc_collected_data = pd.DataFrame(epc_collected_data)

    epc_collected_data = epc_data[["uprn", "address", "address1", "postcode"]].merge(
        epc_collected_data, left_on="uprn", right_on="extracted_uprn"
    )

    elmhurst_surveys = epc_collected_data[
        epc_collected_data["Accreditation scheme"].isin(
            ["NHER", "Stroma Certification Ltd", "Elmhurst Energy Systems Ltd"]
        )
    ]

    quidos_surveys = epc_collected_data[
        epc_collected_data["Accreditation scheme"].isin(
            ["Quidos Limited"]
        )
    ]

    ecmk_surveys = epc_collected_data[
        epc_collected_data["Accreditation scheme"].isin(
            ["ECMK"]
        )
    ]

    # Store the data:
    elmhurst_surveys.to_csv("/Users/khalimconn-kowlessar/Documents/hestia/Customers/AIHA/Elmhurst Surveys.csv")
    quidos_surveys.to_csv("/Users/khalimconn-kowlessar/Documents/hestia/Customers/AIHA/Quidos Surveys.csv")
    ecmk_surveys.to_csv("/Users/khalimconn-kowlessar/Documents/hestia/Customers/AIHA/ECMK Surveys.csv")
