import requests
import datetime as dt
import json
import os

key = os.environ.get('KKEXKEY')

def main(vcs, begin_date):
    repo_uri=vcs
    gha_info = {}
    #this is the entire list of Github 'milestones' grabbed from the API
    gha_info['milestones'] = get_milestone_information(repo_uri)
    #this is the count of milestones that occur after the cutoff date
    gha_info['milestone_count'] = parse_milestones(gha_info['milestones'], begin_date)
    #split_actors(repo_uri, actors_list)
    return gha_info['milestone_count']


#this simple API call has been working for now but may need to be updated as more information is desired
def get_milestone_information(repo_uri):
    repo_uri_list = repo_uri.split('/')
    print(repo_uri_list)
    api_url = "https://api.github.com/repos/" + repo_uri_list[-2]  + "/" + repo_uri_list[-1] + "/milestones"
    headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8', 'Authorization': 'bearer ' + key}
    try:
        response = requests.get(url = api_url, headers=headers)
        response_dict = response.json()
    except:
        print('error with the gha request')
        response_dict = {}
    return response_dict

def parse_milestones(milestones, earliest_date):
    count_of_milestones = 0
    for entry in milestones:
        #if entry date is more recent than the earliest date we're looking at
        # TODO: decide whether to use created_at or updated_at or closed_at
        # problem is that no one closes their milestones?! hardly seems representative?!
        # making this a note here, as both Tamburri and van Meijel use 'closed_at'
        print(entry)
        try:
            if entry['updated_at'] != None:
                if dt.datetime.fromisoformat(entry['updated_at'][:-1]) > earliest_date:
                    count_of_milestones += 1
        except TypeError:
            print("string indices error? or I think maybe they just don't use milestones")
    return count_of_milestones


