from flask import jsonify
from urllib.parse import unquote  # Import unquote to decode the URL-encoded query

def get_course_detail(request, Course):
    data = request.get_json()

    query = data.get('title')
    if query:
        # Decode the URL-encoded string to get the actual title
        decoded_query = unquote(query)
        print(decoded_query)  # You can print the decoded query for debugging purposes
        
        # Now, use the decoded title to filter the courses
        users = Course.query.filter(Course.title == decoded_query).all()
        
        # If courses are found, return them
        if users:
            output = [user.to_dict() for user in users]
            return jsonify({'course': output}), 200
        else:
            return jsonify({"response": "no courses found"}), 404
    else:
        return jsonify({"response": "error: title is required"}), 400
