import requests
import pandas as pd
import Apikeys

APIKEY = Apikeys.SUPPLIERPROD
BASEURL = "https://api2.northpass.com/v2/"
HEADERS = {"accept" : "application/json", "X-Api-Key": APIKEY}
BASEFILE = "~/Downloads/Supplier Academy Course Groupings.csv"

def parse_csv():
    df = pd.read_csv(BASEFILE)
    unique_groups = df['group_uuid'].unique().tolist()
    unique_group_name = df['group_name'].unique().tolist()
    groups = df.groupby('group_uuid')
    for key,item in groups:
        group_course_payload = []
        # print(f"Group UUID is: {key}")
        # print(item)
        for courses, other in item.iterrows():
            # print("Course uuid is: ", other['course_uuid'])
            minipayload = { "type": "courses", "id": other['course_uuid']}
            group_course_payload.append(minipayload)
        final_payload = { "data": group_course_payload }
        group_url = f"https://api2.northpass.com/v2/groups/{key}/relationships/courses"
        gcourse_req = requests.post(group_url, headers=HEADERS, json=final_payload)
        print(gcourse_req.status_code)
        print(gcourse_req.text)

        """
        Sample Payload = { "data": [
        {
            "type": "courses",
            "id": "123123"
        },
        {
            "type": "courses",
            "id": "4567"
        }
    ] }
        """

    # print(f"Total Number of courses: {df['course_name'].nunique()}")
    # print(unique_group_name, unique_groups)

def remove_group_courses(unique_groups):
    """
    From Bartosz Chodyla: There's a non-public endpoint:
                            /v1/group_courses/:id
                            where id is the UUID of a record from the group_courses table. You can get the UUIDs directly from the DB or by calling another non-public endpoint, which is:
                            /v1/groups/:group_uuid/courses
    """
    print(unique_groups)
    for group_uuid in unique_groups:
        print(group_uuid)
        # Get Group Courses
        get_gcourses = f"https://api2.northpass.com/v1/groups/{group_uuid}/courses"
        group_courses = requests.get(get_gcourses, headers=HEADERS)
        gcourse_json = group_courses.json()
        print(gcourse_json)



if __name__ == "__main__":
    parse_csv()
