from collections import Counter, namedtuple
import os
import urllib.request

# prep
tmp = os.getenv("TMP", "/tmp")
tempfile = os.path.join(tmp, 'dirnames')
urllib.request.urlretrieve(
    'https://bites-data.s3.us-east-2.amazonaws.com/dirnames.txt',
    tempfile
)

IGNORE = 'static templates data pybites bbelderbos hobojoe1848'.split()

Stats = namedtuple('Stats', 'user challenge')


# code


def gen_files(tempfile=tempfile):
    """
    Parse the tempfile passed in, filtering out directory names
    (first column) using the last "is_dir" column.

    Lowercase these directory names and return them as a generator.

    "tempfile" has the following format:
    challenge<int>/file_or_dir<str>,is_dir<bool>

    For example:
    03/rss.xml,False
    03/tags.html,False
    03/Mridubhatnagar,True
    03/aleksandarknezevic,True

    => Here you would return 03/mridubhatnagar (lowercased!)
       followed by 03/aleksandarknezevic
    """
    with open(tempfile) as temp_file:
        temp_lines = temp_file.readlines()
        for line in temp_lines:
            (challenge_number, rest) = line.split('/')
            (dir_name, dir_flag) = rest.split(',')
            is_dir = (dir_flag.rstrip('\n') == "True")

            if is_dir:
                yield f"{challenge_number}/{dir_name.lower()}"


def diehard_pybites(files=None):
    """
    Return a Stats namedtuple (defined above) that contains:
    1. the user that made the most pull requests (ignoring the users in IGNORE), and
    2. a tuple of:
        ("most popular challenge id", "amount of pull requests for that challenge")

    Calling this function on the default dirnames.txt should return:

    Stats(user='clamytoe', challenge=('01', 7))
    """
    if files is None:
        files = gen_files()

    users = Counter()
    popular_challenges = Counter()

    for file in files:
        challenge, user = file.split('/')
        if user in IGNORE:
            continue
        users.update([user])
        popular_challenges.update([challenge])

    winning_user = users.most_common(1)[0]
    winning_challenge = popular_challenges.most_common(1)[0]
    winning_stats = Stats(user=winning_user[0], challenge=winning_challenge)
    return winning_stats
