import requests

BASEURL = "https://api.northpass.com/v2/"
ANTHOLOGY = "8ALsk8jDOlynEwn8ScMBSnG87"
HEADERS = {
    "accept": "*/*",
    "X-Api-Key": ANTHOLOGY,
    "content-type": "application/json",
}

def main():
    person_id_list = []
    count = 0
    while True:
        count += 1
        url = (
            BASEURL
            + f"people?filter[email][cont]=%40knowledgestate.edu&limit=100&page={count}"
        )
        response = requests.get(url, headers=HEADERS)
        response = response.json()
        nextlink = response["links"]

        for data in response["data"]:
            person_name = data["attributes"]["name"]
            person = data["id"]
            # print(f"Adding {person_name}'s id to list. ID: {person}")
            person_id_list.append(person)

        if "next" not in nextlink:
            break

    print(f"Cycling through {len(person_id_list)} people and adding their properties.")
    add_ppl_to_groups(person_id_list)

def add_ppl_to_groups(people):
    groups_to_add = ['16dca68e-96e2-4633-aaa6-ae181919ded5','ef60a475-259e-4b69-b008-d2d0b4408b9b']
    group_add_url = f"{BASEURL}bulk/people/membership"
    if len(people) > 1500:
        for chunk in range(0, len(people), 1500):
            i = chunk
            payload_1 = []
            i_to_add = people[i : i + 1500]
            for person in i_to_add:
                payload_1.append(person)
            payload = { "payload": {
                "person_ids": payload_1,
                "group_ids": groups_to_add
            } }
            response = requests.post(group_add_url, headers=HEADERS, json=payload)
            print(f"Completed. Status code is {response.status_code}")

if __name__ == "__main__":
    main()
