import requests
import Apikeys

BASEURL = "https://api.northpass.com/v2/"
APIKEY = Apikeys.ZENJOB
GROUPUUID = "940a5d24-32af-45f1-8ed4-8a6b4689d9c9"
HEADERS = {"accept": "application/json", "X-Api-Key": APIKEY}


def get_ppl_without_email_in_group():
    count = 0
    url = f"groups/{GROUPUUID}/memberships"
    filter = "?filter[name][not_cont]=zenjob.com"
    while True:
        count += 1
        request_url = f"{BASEURL}{url}{filter}&page={count}"
        response = requests.get(request_url, headers=HEADERS)
        response = response.json()
        next = response["links"]

        for ids in response["data"]:
            person_uuid = ids["relationships"]["person"]["data"]["id"]
            remove_from_group(person_uuid)

        if "next" not in next:
            break


def remove_from_group(person_uuid):
    url = f"people/{person_uuid}/relationships/groups"
    request_url = f"{BASEURL}{url}"
    payload = {"data": [{"id": GROUPUUID, "type": "membership-groups"}]}
    response = requests.delete(request_url, json=payload, headers=HEADERS)
    code = response.status_code
    print(f"For person {person_uuid}, the returned code was {code}")


if __name__ == "__main__":
    get_ppl_without_email_in_group()
