import requests
import json
import csv
import pandas as pd
import time
import Apikeys
from termcolor import colored, cprint


KNOWLEDGEGROUPS = [
'3f8dc68e-1458-4199-9641-6781960e085e',
'8585fe89-a050-4dbb-beb8-6ebd7358a970',
'd2542667-0dbf-4680-a5af-042d70f24a55',
'2b5267b2-ce87-4e77-ad88-5cfec80496b9',
'483c3416-ddfb-43fe-983b-08abb6b50c62',
'1582d056-55fb-403b-8a65-f3b641c96b69',
'be95bdcc-e72a-4132-8a67-9dde9bad5e2a',
'0a3412da-5f73-4738-8364-15d5919750f3',
'197da27d-0497-40b5-b2f8-cec4124d32f6',
'a031d9a8-e433-45cf-826a-8881644f8eac',
'02702bf3-261c-41e0-a22d-26d3e90493a3',
'3b149bfe-31c5-4991-bd6c-ba4c760089d4',
'b6ae5e37-db6a-4b79-949f-be73b216f677',
'bfb708e4-18eb-47b5-afde-737f16721e9a',
'f02032d3-3d60-4cb1-acac-855c229646c3',
'96b24666-85f2-4f70-ae59-f5a924cc045f',
'f7701275-cebc-482b-ac31-9cfcd93937c3',
'fcfe4ee2-b247-4244-8cfc-f3d98d219fea',
'c6b6d415-323e-46c1-859e-be86fd36ec48',
'e53216bf-9815-42c7-89c1-953a7b1289a3',
'5eeef2ff-1616-43bb-a0c1-aa84ad551824',
'59ccfdeb-8a8a-4693-b4fa-27034192071c',
'849f1551-604a-4b5c-9b5d-e2771eed488c',
'cf5d1920-9618-43f3-8dac-53954d19a956',
'0a5c0100-9500-46a5-a7be-40d03fc5dfe9',
'4d0bf08e-3dda-4a2e-8213-72a020873a03',
'e48c8995-6a64-45c1-ae62-ba96fcc01542',
'0ef5fdd2-718c-47d2-88bc-2d0193b18530',
'604dd8b8-175a-4a74-93d2-28760f1d1835',
'26c5277c-440a-4dea-b625-beb986cff673',
'8e33adf0-5932-4535-90c7-10fa04e97201',
'1ef34494-4d48-4b69-9819-a22c5870fc24',
'b2b8d7aa-06e8-4ed5-bc9b-cb9ce0e81309',
'e4017ee0-6141-4145-816f-ed68ee6931bc',
'84d32175-8cb8-4fb0-95cc-6ae13d40aaaa',
'27489e34-b04c-410e-99a2-0d93e2e42fbf',
'e5e8565f-80e2-4462-b687-56f6d64f95e4',
'27accc37-c3fd-465f-99cd-3e131081aeca',
'32e112bd-5495-4399-85dd-1925e1ccbba5',
'dc50ca43-5071-45b3-bf42-e1e64416ffd0',
'950a6345-5a13-4931-8d82-eac6adef03e3',
'700640e7-0de3-49dc-b441-4efff8ad33ba',
'5f35e542-a8cf-4422-8e87-466cdca62864',
'f50cb362-2f86-44eb-89e6-bea6ecbaf89f',
'31a7cbe0-6aa6-403b-a561-6bc4fa81c0b1',
'853de4bd-6f6a-4d1d-980a-b67eb1b0e876',
'cd0fa4e0-2d24-4b35-918a-33baa736015e',
'933baf03-3664-4c33-bd97-208a9f7ab78b',
'55bae3db-5f62-4be3-823a-bcb429b8a2b2',
'4754b85b-e7a6-41a8-b0e9-5e02c58ebc38',
'33f4fc73-102d-492e-9b0a-383d0b0f68b0',
'fa8914be-0986-460c-884d-9973a9622045',
'b9f734fa-de0d-4a0b-9ce2-c092126e1d8d',
'448f3335-cf11-4e7a-9939-c734861d16e3',
'106775db-a00d-4956-bf27-97ea269bb001',
'594cd6c0-17db-4241-be56-ad28a8db4f7b',
'95f7b67d-3ba8-4d18-bcbb-3e02f7bfaf7a',
'0323339c-b92b-481c-9249-651ef0273ad7',
'38a42a60-f6e6-4beb-95f1-51c5469dc4c6',
'd694804c-ae1a-4db0-b5fc-2497e43abb6f',
'a008a4e6-e026-4a1c-8aef-eea78c41b029',
'4575114c-1e63-41b1-8953-67d3ce3ed3e6',
]

PERSON_IDS = [
    # THIS LIST IS OUTDATED. I WOULDN'T USE IT.
    # NOT SURE WHY I DON'T JUST DELETE IT
    "f44363ca-eff8-4cc9-b67f-d49fcbd1f20d",
    "07fd80f8-3fe8-4386-84dc-280dade1ed5f",
    "8bffdf96-3947-4f14-8041-c25e4a488b80",
    "a68a6120-067e-477d-89d5-d1da1a96d09f",
    "03c65c74-2cb8-4c65-a712-6f0f48939d8b",
    "56919b58-7be1-4c1b-912f-54887303eefb",
    "b7e51667-d26f-4851-8878-d54bb4ce8a51",
    "45fb5c92-2c3d-4c49-9f3e-949ab66b06e5",
    "ea3706d8-c312-4830-8c18-0b7dcc0285d8",
    "10edf9db-3bec-4beb-9047-44da8e3047ab",
    "3c976bca-3f07-438b-9b8c-953782236666",
    "b36d7d59-fc3c-480a-8d6a-9025e6ee4cbb",
    "5718e19a-82d4-4855-af92-45c5cf8e670f",
    "a3cdfd66-76dc-4e11-89ca-8a7fa32d2a47",
    "123ef7cc-9a29-4a70-9e2c-86567ca02ab8",
    "55498b66-5616-4f7c-a29c-9506e3d537cf",
    "781d706e-3a9f-44dd-acca-2d0570b41544",
    "58028ff3-4fb3-453b-8cc3-14c831977da4",
    "56d52d1f-7eee-4f32-ac89-7bbf6c668cdd",
    "baf1c792-3a87-4357-a93b-871eaabc0ab0",
    "00969b3e-de91-4133-aa05-421a75651508",
    "87c4fed6-166e-447a-b542-fb0403188fba",
    "c84a13e9-950f-4109-a7e4-a2412fb6889b",
    "fd6bc3ff-5416-418f-928c-4d1a5e96c651",
    "43a20d23-69bd-4307-ad60-d2832ab81b39",
    "1d579f1e-cae7-4866-9669-19074dfe8793",
    "7723fe1b-bd33-4b89-84fc-7218303e2bad",
    "3a8a77c0-eb96-4148-bdb8-6854c4b55129",
    "f8410b69-f5e7-4fbe-b89c-a405173570ac",
    "82adc395-7141-4de2-9b5f-a9c128bdd00f",
    "8522f536-b77a-4c4b-8025-d6a10410f20a",
    "eb857569-7fde-47f0-b404-db1a3ace1a19",
    "daf7691c-5391-4f79-9da6-3ec383f955f0",
    "242df255-14d0-41b8-ad57-afa4e788bdcf",
    "1d8ffc86-80f5-4d42-8d3f-c705ee2e1482",
    "23da9e58-b7fc-40cc-bacb-5ec98b9da3df",
    "f4da7725-9c24-4d63-ba9b-fd1962f1fd4e",
    "e6954ff8-2fcb-4044-bc8f-423de5b0911d",
    "f7ec7cf5-bf70-4af6-b4bb-09891362df3f",
    "d0648191-211e-4800-8526-91b2e8019b49",
    "e4c08d0e-df51-432a-bc6a-648e0cfcb31f",
    "415515be-c141-4002-83b1-6a6466507278",
    "998508ee-fc6e-4770-a7b7-19b34eab3f0b",
    "d790b773-2ceb-4cc7-8011-05d1f3a83b1f",
    "33921abd-474a-469b-a114-a8f5bfec8a1a",
    "4b7f75e4-d708-45d6-82e8-17e6ab98ba69",
    "07f30e75-5a77-4b29-897b-caf9833b526b",
    "303fccc3-cf87-4890-987a-4c246514a4cb",
    "e0c85f8e-e03d-43b0-94dc-1aaf573a09b0",
    "2197fdea-28c7-40f2-80d3-067617511c7f",
    "cd844391-f786-4ea1-bccb-80b1b3a88eba",
    "9206f9d4-6a43-4418-bfa4-fdc2e80cb601",
    "222f8edc-cdca-4638-b93b-57b62cca9bc9",
    "2ee1c1d9-d332-49c0-b186-50dbdffcc5da",
    "a30b533d-c1b6-44ce-a67f-31a1f20a2bf6",
    "603be042-5434-4482-a1a5-ba3f4f351b0b",
    "53a70521-9bab-4177-9ec7-21d38ec52da3",
    "8aa5d835-2a94-437e-9482-8cefbc7f73c5",
    "5f8c5f02-722c-46a6-b6cd-7fc991ba91fe",
    "9a69f913-d065-4bd1-8c8f-306b99253077",
    "6eeacf18-15c7-46c4-98c9-e4cc38d4d927",
    "f8e2668e-da42-426d-bfd7-7302958acaa8",
    "575ba093-7bf8-4702-a46d-27a3783310ea",
    "f6f2f233-24db-4541-ab55-813c547b3132",
    "05e5d881-3896-4ba1-9a78-7eac2518d89c",
    "3d73474f-5b7e-443a-9a9e-be33333d4a82",
    "c2bae366-64c9-4bd0-a7d1-251971b65c61",
    "32db9bd3-30aa-4b7f-add3-27adc42db1be",
    "01514376-77b2-4432-8ee4-6f303969318d",
    "3f3e16ac-28da-4d5d-803a-97f3bc4cda8f",
    "d56bbe1d-2d58-4fea-a84e-3eab4d30f665",
    "044fb66b-2232-415d-bc47-1c309419ff4a",
    "f528c291-d2e6-4fd3-9253-24fd9a2a04b3",
    "117f2aaf-30bb-4b92-b9b2-42a8ae0d72e9",
    "d334a708-4512-4550-ac1e-0d0b1d4020b9",
    "ee18a795-4c0e-4e3a-accf-9018035a0195",
    "5acff021-3b6e-4313-a07c-6a000791a093",
    "71ce9d84-ebd2-4a4c-a074-78ec9070fd22",
    "38a5c677-4443-4d84-8e2d-3cdae3aa0648",
    "cff1bfd8-ebbf-4f63-9b9b-95ffea97fbbc",
    "cfedb93c-e5f9-48d8-9759-bbfed6b80c36",
    "c2a40abd-ccc2-47af-86a0-2fdd616e5612",
    "5b12d62b-47f1-461b-88c7-022f50430e77",
    "a83219ba-1124-4dae-a0f1-73ec6e06bf0a",
    "9367ee67-05e5-4663-a394-2cd3e91655e3",
    "0633bb08-c971-4fd2-9849-99b20ae1a25c",
]

APIKEY = Apikeys.ANTHOLOGY
BASEURL = "https://api.northpass.com/v2/"
HEADERS = {"accept": "application/json", "X-Api-Key": APIKEY}
GROUPS = []
BASEFILE = "/Users/normrasmussen/Downloads/Anthology_master_backup.csv"


def grab_person_group_ids():
    rowdict = {"knowledgestate.edu": KNOWLEDGEGROUPS}
    grab_ppl_ids(rowdict)


def grab_ppl_ids(rowdict):
    cprint(f"The dictionary is grabbing all the groups. Here's the dict: {rowdict}", 'green')
    page_count = 0
    person_list = []
    while True:
        # for domain, group_list in rowdict.items():
        # Grab all people
        page_count += 1
        url = (
            BASEURL
            + f"people/?filter[email][cont]=@knowledgestate.edu&limit=100&page={page_count}"
        )
        response = requests.get(url, headers=HEADERS)
        resp = response.json()
        nextlink = resp["links"]

        for data in resp["data"]:
            person = data["id"]
            person_list.append(person)

        if "next" not in nextlink:
            break

    if len(person_list) > 0:
        cprint(f"Person list for knowledgestate.edu has {len(person_list)} people.", 'blue')
        bulk_remove_and_enroll(person_list, KNOWLEDGEGROUPS)
    else:
        cprint(f"Person list for knowledgestate.edu has {len(person_list)} people.", 'blue')
        cprint(f"Skipping the bulk function.", 'yellow')


def bulk_remove_and_enroll(person_list, group_list):
    cprint("Moving people and groups into bulk function.", 'green')
    COUNT = 0
    FINISH_SIGNAL = len(person_list)
    # Get people with groups and remove them from those groups
    for person in person_list:
        COUNT += 1
        url = BASEURL + f"people/{person}"
        response = requests.get(url, headers=HEADERS)
        data = response.json()
        groups = data["data"]["relationships"]["groups"]
        name = data["data"]["attributes"]["full_name"]
        del_group_list = []
        for group in groups["data"]:
            del_payload_var = {"id": group["id"], "type": "membership-groups"}
            del_group_list.append(del_payload_var)
        del_payload_base = {"data": del_group_list}
        if not del_payload_base["data"]:
            pass
        else:
            url = BASEURL + f"people/{person}/relationships/groups"
            response = requests.delete(url, headers=HEADERS, json=del_payload_base)
            good_status_codes = [202, 204, 200, 203]
            if response.status_code in good_status_codes:
                pass
            else:
                cprint(f"Error: {response.status_code} with {name}", 'red')

    if COUNT == FINISH_SIGNAL:
        # Since we're de-enrolling one by one, let's sleep and wait.

        cprint("Sleeping for 2 seconds.", 'yellow')
        time.sleep(2)
        cprint("Sleep Complete. Hold on to your butts!", 'yellow')

        # Re-enroll everyone back into all the groups by creating subset groups
        # Trying to do everyone at once (143 ppl * 54 groups) resulted in payloads that are too big
        # Doing the subsets of 25 ppl each did not yield any errors.
        composite_list = [person_list[x:x+25] for x in range(0, len(person_list),25)]
        for people_subset in composite_list:
            payload = {"payload": {"person_ids": people_subset, "group_ids": group_list}}
            cprint(f"{payload}", 'green')
            url = BASEURL + "bulk/people/membership/"

        # The above is commented out because I kept getting a 413 error of too much content
        # Changing this to enroll each person.... one at a time.
        # But this is slow and didn't work. It actually just stopped working after around 30 people.
        # miniload = []
        # for groupuuid in group_list:
        #     tmpload = {"type":"membership-groups","id":groupuuid}
        #     miniload.append(tmpload)
        #
        # print(len(person_list))
        # for person in person_list:
        #     url = BASEURL + f"people/{person}/relationships/groups"
        #     payload = { "data": miniload }
            try:
                cprint(f"Trying for person: {person}", 'green')
                cprint(f"With payload: {payload}", 'blue')
                response = requests.post(url, headers=HEADERS, json=payload)
                response.raise_for_status()
            except requests.exceptions.HTTPError as err:
                cprint(f"Error: {response.status_code}. Exception: {err}", 'red')
            except requests.exceptions.Timeout:
                cprint("Timeout Error", 'red')
            except requests.exceptions.TooManyRedirects:
                cprint("Too Many Redirects Error", 'red')
            except requests.exceptions.ChunkedEncodingError as ex:
                cprint(f"Invalid chunk encoding {str(ex)}", 'yellow')
            finally:
                cprint(response.status_code, 'yellow')
                cprint(response.text, 'yellow')
                cprint("Okay, let's see how that went.", 'red')

if __name__ == "__main__":
    grab_person_group_ids()
