from collections import defaultdict
from flask import jsonify
from analysis_user_activity import analyze_user_activity
from database import db_connection

def fetch_as_dict(cursor):
    """ Helper function to fetch rows as dictionaries """
    columns = [col[0] for col in cursor.description]
    rows = cursor.fetchall()
    result = [dict(zip(columns, row)) for row in rows]
    return result

def fetch_user(request):
    conn = db_connection()
    
    # Get parameters from the request
    # course = request.values.get('course')
    query = request.values.get('query')
    if (query == None):
        query = ''
        

    # Initialize cursor
    cursor = conn.cursor()

    # Fetch featured courses
    cursor.execute("SELECT * FROM users WHERE fullname LIKE %s")
    users = fetch_as_dict(cursor,(query))

    # Build the final response structure
    final_response = {
        "users": users,
       
    }
    
    return jsonify(final_response), 200
