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 = [
]

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()
