import Apikeys
import requests
import pprint
import csv

pp = pprint.PrettyPrinter(indent=4)
APIKEY = Apikeys.EMPLOY
HEADERS = {
    "accept": "application/json",
    "X-Api-Key": APIKEY,
}
GROUPS = [
]

def get_courses():
    """
    Function to get courses and their IDs
    """
    count = 0
    list_of_ids = []

    while True:
        # for course_name in COURSES:
        count += 1
        # url = f"https://api.northpass.com/v2/courses/?filter[name][eq]={course_name}"
        url = f"https://api.northpass.com/v2/courses/?limit=100&page={count}"
        response = requests.get(url, headers=HEADERS)
        response = response.json()
        nextlink = response["links"]

        for item in response["data"]:
            status = item['attributes']['status']
            if status == 'live':
                name = item['attributes']['name']
                print(name)
                idict = {item["attributes"]["name"]: item["id"]}
                list_of_ids.append(idict)
                # if "[Core]" in name:

        if "next" not in nextlink:
            break

    pp.pprint(list_of_ids)
    # print(len(list_of_ids))
    # with open(
    #     "/Users/normrasmussen/Downloads/LuminateUS-Courses.csv", "a+", newline='\n'
    # ) as csvfile:
    #     for group in list_of_ids:
    #         for key, value in group.items():
    #             csvwriter = csv.writer(csvfile)
    #             csvwriter.writerow([key, value])


if __name__ == "__main__":
    get_courses()
