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",
}
KNOWLEDGEGROUPS = [
    "289f8d54-e487-48a0-ac43-5c51c1d0b151",
    "a71e6ca9-aaa5-45a6-9e23-3b4c2a707c3d",
    "b69916db-59e3-4b06-a5f1-d358338ed7d7",
    "3875a210-f86d-4b4a-a86a-980022374936",
    "c482c095-4d67-4673-a175-ab27a5bd959b",
    "4992b00e-0453-4060-9451-fa963f56150e",
    "3f8dc68e-1458-4199-9641-6781960e085e",
    "8585fe89-a050-4dbb-beb8-6ebd7358a970",
    "d2542667-0dbf-4680-a5af-042d70f24a55",
    "7395831e-4325-4b16-85bb-36c94f68aec0",
    "180571e8-f943-4980-8997-b3eed2a0c141",
    "2b69f2f8-d567-48c3-8bb3-22e0dc8819bd",
    "2b5267b2-ce87-4e77-ad88-5cfec80496b9",
    "483c3416-ddfb-43fe-983b-08abb6b50c62",
    "df9855d7-12ae-4170-acf6-a05cd4f52515",
    "1582d056-55fb-403b-8a65-f3b641c96b69",
    "be95bdcc-e72a-4132-8a67-9dde9bad5e2a",
    "299dd454-076d-4580-8cb9-4301997e86cd",
    "0a3412da-5f73-4738-8364-15d5919750f3",
    "197da27d-0497-40b5-b2f8-cec4124d32f6",
    "a031d9a8-e433-45cf-826a-8881644f8eac",
    "02702bf3-261c-41e0-a22d-26d3e90493a3",
    "a013fa2c-5b84-49d3-a3b4-fb80a768f37c",
    "c5ea5c83-9535-4d52-a2d0-12c101e3aee9",
    "672a0dd4-a5a0-43c1-bd40-f66cdb241ec7",
    "3b149bfe-31c5-4991-bd6c-ba4c760089d4",
    "b6ae5e37-db6a-4b79-949f-be73b216f677",
    "2f787b42-e43a-4cea-9c12-19a647e21eeb",
    "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",
    "c78fffea-32b8-477e-81e9-7133119d9207",
    "59ccfdeb-8a8a-4693-b4fa-27034192071c",
    "849f1551-604a-4b5c-9b5d-e2771eed488c",
    "cf5d1920-9618-43f3-8dac-53954d19a956",
    "4d0bf08e-3dda-4a2e-8213-72a020873a03",
    "e48c8995-6a64-45c1-ae62-ba96fcc01542",
    "0ef5fdd2-718c-47d2-88bc-2d0193b18530",
    "8302b674-c728-42d2-9ba3-908b4d970436",
    "ab461de6-1137-4548-87bf-f9dd78905417",
    "0b26d479-d9a7-4e07-aa2f-4893467f41c2",
    "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",
    "65cff7cb-0a29-4c0c-9280-76af55d1432d",
    "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",
    "d694804c-ae1a-4db0-b5fc-2497e43abb6f",
    "a008a4e6-e026-4a1c-8aef-eea78c41b029",
    "4575114c-1e63-41b1-8953-67d3ce3ed3e6",
    "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",
]
KNOWLEDGEPROPS = "Anthology Academic Economics: Essential, Anthology Academic Economics: Enhanced, Anthology Accreditation: Essential, Anthology Accreditation: Enhanced, Anthology 101: Essential, Anthology Baseline: Essential, Anthology Baseline: Enhanced, Anthology Beacon: Essential, Anthology Course Evaluations: Essential, Anthology Course Evaluations: Enhanced, Anthology Digital Assistant: Essential, Anthology Digital Assistant: Enhanced, Anthology Encompass: Essential, Anthology Encompass: Enhanced, Anthology Encompass: Enhanced+, Anthology Engage: Essential, Anthology Engage: Enhanced, Anthology Engage: Enhanced+, Anthology Evaluate: Essential, Anthology Evaluate: Enhanced, Anthology Finance & HCM: Essential, Anthology Finance & HCM: Enhanced, Anthology Finance & HCM: Enhanced+, Anthology Insight: Essential, Anthology Insight: Enhanced, Blackboard Learn: Essential, Anthology Milestone: Essential, Anthology Milestone: Enhanced, Outcomes: Essential, Outcomes: Enhanced, Anthology Payroll: Essential, Anthology Payroll: Enhanced, Anthology Planning: Essential, Anthology Planning: Enhanced, Anthology Portfolio: Essential, Anthology Portfolio: Enhanced, Power BI: Essential, Power BI: Enhanced, Anthology Program Review: Essential, Anthology Program Review: Enhanced, Anthology Raise: Essential, Anthology Raise: Enhanced, Anthology Raise: Enhanced+, Anthology Reach: Essential, Anthology Reach: Enhanced, Anthology Reach: Enhanced+, Anthology Student: Essential, Anthology Student: Enhanced"


def groups():
    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)


def props():
    person_id_list = []
    count = 0
    while True:
        count += 1
        url = (
            BASEURL
            + f"?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.")
    for person_id in person_id_list:
        propsurl = "https://api.northpass.com/v2/properties/people/bulk"
        payload = {
            "data": [
                {
                    "attributes": {
                        "properties": {"subscription_levels": KNOWLEDGEPROPS}
                    },
                    "id": person_id,
                    "type": "person_properties",
                }
            ]
        }
        prop_response = requests.post(propsurl, headers=HEADERS, json=payload)
        print(f"{person_id}'s status code is {prop_response.status_code}'")
        if prop_response.status_code != 200:
            print(
                    f"There is a non-200 status code. The code was {prop_response.status_code}. Here's the text:"
            )
            print(f"{response.text}")


if __name__ == "__main__":
    props()
