import pandas as pd
import requests
import Apikeys

MASTER = "~/Downloads/Anthology-Master-CSV-FirstChanges.csv"
BASEURL = "https://api.northpass.com/v2/people"
APIKEY = Apikeys.ANTHOLOGY
HEADERS = {
    "accept": "*/*",
    "X-Api-Key": APIKEY,
    "content-type": "application/json",
}
df = pd.read_csv(MASTER)
df.dropna(how="all", axis=0, inplace=True)
COUNT = 0

for row in df.itertuples():
    domain = row[1]
    groups = row[2:]
    # groups = list(groups)
    tmplist = []
    for group in groups:
        group = str(group)
        if "nan" not in group:
            # Grab Group UUIDs
            url = f"https://api.northpass.com/v2/groups?filter[name][eq]={group}"
            response = requests.get(url, headers=HEADERS)
            response = response.json()
            data = response["data"]
            for name in data:
                id = name["id"]
                tmplist.append(id)
    rowdict = {domain: tmplist}

    # Grab all people
    personlist = []
    COUNT += 1
    url = BASEURL + f"?filter[email][cont]={domain}&limit=100"
    response = requests.get(url, headers=HEADERS)
    response = response.json()
    nextlink = response["links"]

    for data in response["data"]:
        person = data["id"]
        personlist.append(person)

    # if "next" not in nextlink:
    #     break

    # Construct Payload for Bulk API
    payload = {"payload": {"person_ids": personlist, "group_ids": tmplist}}
    print(payload)
