class Exercises:
    def __init__(self, db_connection_method):
        self.execute = db_connection_method

    def get(self, query):
        # Add wildcards to the query
        search_query = f"%{query}%"
        exercises = self.execute("SELECT exercise_id, name FROM exercise WHERE LOWER(name) LIKE LOWER(%s) ORDER BY name ASC;", [search_query])
        return exercises
    
    def get_exercise(self, exercise_id):
        exercise = self.execute("SELECT exercise_id, name FROM exercise WHERE exercise_id=%s;", [exercise_id], one=True)
        return exercise
    
    def update_exercise_name(self, exercise_id, updated_name):
        self.execute("UPDATE exercise SET name = %s WHERE exercise_id = %s;", [updated_name, exercise_id], commit=True)
        updated_exercise = self.get_exercise(exercise_id)
        return updated_exercise
    
    def delete_exercise(self, exercise_id):
        self.execute('DELETE FROM exercise WHERE exercise_id=%s', [
            exercise_id], commit=True)
    
    def add_exercise(self, name):
        result = self.execute('INSERT INTO exercise (name) VALUES (%s) RETURNING exercise_id', [name], commit=True, one=True)
        exercise_id = result['exercise_id']
        new_exercise = self.get_exercise(exercise_id)
        return new_exercise

