from flask import Flask, render_template, request
import xml.etree.ElementTree as ET

app = Flask(__name__)

def load_questions():
    tree = ET.parse('questions.xml')
    root = tree.getroot()
    
    questions = []
    for question in root.findall('question'):
        q = {
            'number': question.get('number'),
            'text': question.get('text'),
            'options': []
        }
        for option in question.findall('option'):
            q['options'].append({
                'letter': option.get('letter'),
                'trait': option.get('trait'),
                'text': option.text
            })
        questions.append(q)
    return questions

@app.route('/')
def assessment():
    questions = load_questions()
    return render_template('assessment.html', questions=questions)

@app.route('/results', methods=['POST'])
def results():
    questions = load_questions()
    scores = {'D': 0, 'I': 0, 'S': 0, 'C': 0}
    
    for q in questions:
        answer = request.form.get(f'q{q["number"]}')
        if answer in scores:
            scores[answer] += 1
    
    dominant = max(scores, key=scores.get)
    total = sum(scores.values())
    
    return render_template('results.html',
                         scores=scores,
                         dominant=dominant,
                         total=total)

if __name__ == '__main__':
    app.run(debug=True)