import argparse
import json
from slugify import slugify

if __name__ == "__main__":

    parser = argparse.ArgumentParser()

    parser.add_argument("--suggestion_file", type=str, required=True, help="The suggestion file generated by suggest_reviewers.py")
    parser.add_argument("--softconf_file", type=str, required=True, help="The output file for softconf")
    parser.add_argument("--split_by_track", action='store_true', help='Separate assignments by track')

    args = parser.parse_args()

    # Load the data
    with open(args.suggestion_file, "r") as f:
        submissions = [json.loads(x) for x in f]

    by_track = {}
    with open(args.softconf_file, 'w') as f:
        for submission in submissions:
            sid = submission['startSubmissionId']
            line = f'{sid}:'+':'.join([x['startUsername'] for x in submission['assignedReviewers']])
            print(line, file=f)

            if args.split_by_track:
                if submission['track'] not in by_track:
                    g = open(f'{args.softconf_file}.{slugify(submission["track"])}', 'w') 
                    by_track[submission['track']] = g
                print(line, file=by_track[submission['track']])

    if args.split_by_track:
        for g in by_track.values():
            g.close()