import csv
import os
import Apikeys
import requests

APIKEY = Apikeys.walmartprod
CMD = "touch ~/Downloads/Spark_Categories.csv"
HEADERS = {
    "accept": "application/json",
    "X-Api-Key": APIKEY,
}


def get_categories():
    """
    Function to get the category Name and IDs and adding them to a list of tuples
    """
    os.system("touch ~/Downloads/Spark_Categories.csv")
    url = "https://api2.northpass.com/v2/categories?limit=100"
    response = requests.get(url, headers=HEADERS)
    response = response.json()

    for item in response["data"]:
        cat_tupe = (item["id"], item["attributes"]["name"])
        with open(
            "/Users/normrasmussen/Downloads/Spark_Categories.csv", "a", newline=""
        ) as csvfile:
            writer = csv.writer(
                csvfile, delimiter=",", quotechar="|", quoting=csv.QUOTE_MINIMAL
            )
            writer.writerow(cat_tupe)


def get_courses():
    """
    Function to get the course name, ids, and activity names and ids to a list.
    """

    count = 0

    while True:
        count += 1
        url = f"https://api2.northpass.com/v2/courses?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":
                courses = (item["id"], item["attributes"]["name"])
                get_activities(courses)

        if "next" not in nextlink:
            break


def get_activities(courses):
    """
    Function that exists within a for loop!
    This function is to get the category Name and IDs and adding them to a list of tuples
    and writes them to a csv
    """
    os.system("touch ~/Downloads/Spark_Courses.csv")
    course_list = []
    course_uuid, _ = courses
    url = f"https://api2.northpass.com/v2/courses/{course_uuid}/activities"
    response = requests.get(url, headers=HEADERS)
    response = response.json()

    for data in response["data"]:
        activity_tupe = (data["id"], data["attributes"]["title"])
        course_list.append(activity_tupe)
    courses = (courses, course_list)
    with open("/Users/normrasmussen/Downloads/Spark_Courses.csv", "a") as csvfile:
        writer = csv.writer(
            csvfile, delimiter=",", quotechar="|", quoting=csv.QUOTE_MINIMAL
        )
        writer.writerow(courses)


def get_learning_paths():
    """
    Function to get the Learning Path Name and IDs and adding them to a list of tuples
    """
    os.system("touch ~/Downloads/Spark_LPs.csv")
    url = "https://api2.northpass.com/v2/learning_paths"
    response = requests.get(url, headers=HEADERS)
    response = response.json()

    for data in response["data"]:
        lps = (data["id"], data["attributes"]["name"])
        print(lps)
        with open("/Users/normrasmussen/Downloads/Spark_LPs.csv", "a") as csvfile:
            writer = csv.writer(
                csvfile, delimiter=",", quotechar="|", quoting=csv.QUOTE_MINIMAL
            )
            writer.writerow(lps)


if __name__ == "__main__":
    get_categories()
    get_courses()
    get_learning_paths()
