import requests
import Apikeys

apiKey = Apikeys.client
groups = [
    "Armonk",
    "Babylon",
    "Bayside",
    "Bedford",
    "Cutchogue",
    "East Hampton",
    "East Setauket",
    "Executive",
    "Farmingville",
    "Franklin Square",
    "Garden City",
    "Great Neck",
    "Greenport",
    "Hampton Bays",
    "Hamptons",
    "Huntington",
    "Huntington Station",
    "Katonah",
    "Locust Valley",
    "Long Beach",
    "Long Island",
    "Long Island City",
    "Manhasset",
    "Manhattan",
    "Massapequa Park",
    "Mattituck",
    "Merrick",
    "Montauk",
    "New Hyde Park",
    "New York",
    "North Fork",
    "Ocean Beach",
    "Plainview",
    "Port Washington",
    "Queens",
    "Quogue",
    "Rockville Centre",
    "Roslyn",
    "Sag Harbor",
    "Sayville",
    "Scarsdale",
    "Sea Cliff",
    "Smithtown",
    "Southampton",
    "Syosset",
    "Westchester",
    "Westhampton Beach",
]


def findgroupIDs(apiKey, groups):
    groupuuids = []
    badGroups = []
    for group in groups:
        url = "https://api.northpass.com/v2/groups?filter[name][eq]="
        headers = {"accept": "application/json", "X-Api-Key": apiKey}
        response = requests.get(url + group, headers=headers)
        response = response.json()
        try:
            response = response["data"][0]["id"]
            groupuuids.append(response)
        except:
            badGroups.append(group)
        finally:
            pass
    # print(badGroups)
    # print(groupuuids)
    assignCourse(apiKey, url, groupuuids)


def assignCourse(apiKey, url, groupuuids):
    for group in groupuuids:
        try:
            url = f"https://api.northpass.com/v2/groups/{group}/relationships/courses"
            courseuuid = "10e9b174-cbad-4caa-b0ff-1a7338f2345a"
            headers = {
                "accept": "application/json",
                "content-type": "application/json",
                "X-Api-Key": apiKey,
            }
            payload = {"data": [{"type": "courses", "id": courseuuid}]}
            print(f"Group {group} successfully accepted the course")
            response = requests.post(url, json=payload, headers=headers)
        except:
            print(f"Group {group} does not have that course, yet")
        finally:
            pass


if __name__ == "__main__":
    findgroupIDs(apiKey, groups)
