import { Request, Response } from 'express';
import { ExerciseService } from '../services/exercise.service';
import { sendSuccess, sendError } from '../utils/apiResponse';
import logger from '../utils/logger';

export class ExerciseController {
    static async getAllExercises(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const includeArchived = req.query.includeArchived === 'true';
            const exercises = await ExerciseService.getAllExercises(userId, includeArchived);
            return sendSuccess(res, exercises);
        } catch (error) {
            logger.error('Error in getAllExercises', { error });
            return sendError(res, 'Server error', 500);
        }
    }

    static async getLastSet(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const exerciseId = req.params.id;
            const lastSet = await ExerciseService.getLastSet(userId, exerciseId);
            return sendSuccess(res, { set: lastSet });
        } catch (error) {
            logger.error('Error in getLastSet', { error });
            return sendError(res, 'Server error', 500);
        }
    }

    static async saveExercise(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const exercise = await ExerciseService.saveExercise(userId, req.body);
            return sendSuccess(res, exercise);
        } catch (error) {
            logger.error('Error in saveExercise', { error });
            return sendError(res, 'Server error', 500);
        }
    }
}
