from .data import load

def render_questions() -> list:
    data = load('questions.json')
    for question in data:
        _answers = [ answer['text'] for answer in question['answers'] ]
        question['answers'] = _answers
        question.pop('max', None)
    return data

def evaluate_answers(answers:list) -> dict:
    playbooks = load('playbooks.json')
    questions = load('questions.json')
    scores = dict.fromkeys(playbooks,0)
    for index, answer in enumerate(answers):
        question = questions[index]
        if type(answer) is list:
            answer = answer[0:question['select']]
            for _answer in answer:
                for match in question['answers'][int(_answer)]['matches']: scores[match] += 1
        else:
            for match in question['answers'][int(answer)]['matches']: scores[match] += 1
    return scores    

def compile_results(results:dict) -> dict:
    output = {
        'all_playbooks': results.copy(),
        'playbooks': [ ],
        'score': max(results.values()),
        'max_score': 0
    }
    for question in load('questions.json'):
        output['max_score'] += question.pop('max', 1)
    playbooks = load('playbooks.json')
    for playbook, score in results.items():
        if score == output['score']: output['playbooks'].append({playbook: playbooks[playbook].copy()})
    return output