from flask import Flask, session, redirect, url_for, request, render_template
from flask_session import Session

# Initialize Flask application
app = Flask(__name__)

# Configure the app
app.config['SECRET_KEY'] = 'your-secret-key'  # Used for securely signing the session
app.config['SESSION_TYPE'] = 'filesystem'  # Store session data on the filesystem
Session(app)  # Initialize session handling

# Define the questions for the core transformation process
questions = [
    "Select an issue you want to work with.",
    "Ask this part of you: What do you want?",
    "If you have already achieved {previous answer}, fully and completely, what do you want through having that that is even more important?"
]

# Define a list of core states
core_states = ['peace', 'love', 'joy', 'unity', 'wholeness']

def is_core_state(answer):
    """Check if the given answer represents a Core State."""
    return any(state in answer.lower() for state in core_states)

@app.route('/', methods=['GET', 'POST'])
def start():
    """Handle the start page of the application."""
    if request.method == 'POST':
        session.clear()  # Clear any existing session data
        session['current_question'] = 0  # Start with the first question
        session['answers'] = []  # Initialize an empty list to store answers
        session['reached_core_state'] = False  # Initialize the core state flag
        return redirect(url_for('question'))  # Redirect to the question route
    return render_template('start.html')  # Render the start page template


@app.route('/question', methods=['GET', 'POST'])
def question():
    """Handle the question page of the application."""
    if 'current_question' not in session:
        return redirect(url_for('start'))

    if request.method == 'POST':
        if 'core_state' in request.form:
            session['reached_core_state'] = True
            return redirect(url_for('result'))

        answer = request.form['answer']  # Get the user's answer from the form
        session['answers'].append(answer)  # Store the answer in the session

        # Move to the next question, or keep asking the core question
        session['current_question'] += 1

        if session['current_question'] >= len(questions):
            session['current_question'] = 2  # Repeat the core question

    # Get the current question number
    current_question = session['current_question']

    # For the repeating core question, insert the previous answer into the question
    if current_question >= 2:
        previous_answer = session['answers'][-1]
        question_text = questions[current_question].format(previous_answer=previous_answer)
    else:
        question_text = questions[current_question]

    # Render the question template with the current question and the "I've reached a core state" button after the 2nd question
    return render_template('question.html', 
                           question=question_text, 
                           question_number=current_question + 1,
                           show_core_state_button=(current_question >= 2))


@app.route('/result')
def result():
    """Handle the result page of the application."""
    if 'answers' not in session:
        return redirect(url_for('start'))
    return render_template('result.html', answers=session['answers'], core_state=session['reached_core_state'])


# Run the application if this script is executed directly
if __name__ == '__main__':
    app.run(debug=True)  # Run in debug mode for development


Improve this app such that it follows the Core Transformation process