import requests
import Apikeys
import csv

apikey = Apikeys.talkspace_1099
url_email = "https://api.northpass.com/v2/people?filter[email][eq]="
import_list = "/Users/normrasmussen/Downloads/Compliance Payment - April.csv"

def getfromEmail(url_emil, apikey, import_list):
    with open(import_list, newline='') as csvfile:
        file = csv.reader(csvfile, delimiter=',')
        for items in file:
            email = items[0]
            url = url_email + 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"]
                    items.append(uuid)
                    add_multi_prop(items)
            else:
                print("Another Error!")

def add_multi_prop(items):
    email = items[0]
    role = items[1]
    id_num = items[2]
    paid = items[3].lower()
    # paid_bool = bool("TRUE")
    uuid = items[4]
    url = "https://api.northpass.com/v2/properties/people/bulk"
    payload = {"data": [
            {
                "type": "person_properties",
                "attributes": {"properties": {
                        "paid": paid,
                        "role_type": role,
                        "user_id": id_num
                    }},
                "id": uuid
            }
        ]}
    headers = {
            "accept": "application/json",
            "content-type": "application/json",
            "X-Api-Key": apikey}
    response = requests.post(url, json=payload, headers=headers)

    if response.status_code == 404 or response.status_code == 403:
        print(f"Error {response.status_code} with user {email}")
    else:
        print(f"Successfully Added props for {email}!")

if __name__ == "__main__":
    getfromEmail(url_email, apikey, import_list)
