import requests
import Apikeys
import csv

apiKey = Apikeys.talkspace_1099
baseUrlemail = "https://api.northpass.com/v2/people?filter[email][eq]="
import_list = "/Users/normrasmussen/Downloads/Deactivate Providers - 5.1.23 - Sheet1.csv"

def getfromEmail(baseUrlemail, apiKey, import_list):
    with open(import_list, newline='') as csvfile:
        file = csv.reader(csvfile, delimiter=' ')
        for email in file:
            email = str(email).strip("['']")
            url = baseUrlemail + f"{email}"
            headers = {
                "accept": "*/*",
                "x-api-key": apiKey,
                "content-type": "application/json",
            }
            response = requests.get(url, headers=headers)
            if response.status_code == 200:
                response = response.json()
                for data in response["data"]:
                    uuid = data["id"]
                    deactivate(email, uuid, apiKey)
            else:
                print("Another Error!")


def deactivate(email, uuid, apiKey):
    print(f"Deactivating user {email} with uuid {uuid}.")
    url = f"https://api.northpass.com/v2/people/{uuid}/deactivations"
    headers = {"accept": "application/json","x-api-key": apiKey}
    response = requests.post(url, headers=headers)
    if response.status_code == 404 or response.status_code == 403:
        print(f"Error {response.status_code} with uuid {uuid}")
    else:
        print("Successfully deactivated!")

if __name__ == "__main__":
    getfromEmail(baseUrlemail, apiKey, import_list)
