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

export class SessionController {
    static async getAllSessions(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const sessions = await SessionService.getAllSessions(userId);
            return sendSuccess(res, sessions);
        } catch (error) {
            logger.error('Error in getAllSessions', { error });
            return sendError(res, 'Server error', 500);
        }
    }

    static async saveSession(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const session = await SessionService.saveSession(userId, req.body);
            return sendSuccess(res, session);
        } catch (error: any) {
            if (error.message === 'An active session already exists') {
                return sendError(res, error.message, 400);
            }
            logger.error('Error in saveSession', { error });
            return sendError(res, 'Server error', 500);
        }
    }

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

    static async getLastSession(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const session = await SessionService.getLastWorkoutSession(userId);
            return sendSuccess(res, { session });
        } catch (error) {
            logger.error('Error in getLastSession', { error });
            return sendError(res, 'Server error', 500);
        }
    }

    static async updateActiveSession(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const session = await SessionService.updateActiveSession(userId, req.body);
            return sendSuccess(res, { session });
        } catch (error: any) {
            if (error.message === 'Session not found') {
                return sendError(res, error.message, 404);
            }
            logger.error('Error in updateActiveSession', { error });
            return sendError(res, 'Server error', 500);
        }
    }

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

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

    static async logSetToActiveSession(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const result = await SessionService.logSetToActiveSession(userId, req.body);
            return sendSuccess(res, result);
        } catch (error: any) {
            if (error.message === 'No active session found') {
                return sendError(res, error.message, 404);
            }
            logger.error('Error in logSetToActiveSession', { error });
            return sendError(res, 'Server error', 500);
        }
    }

    static async updateSet(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const { setId } = req.params;
            const updatedSet = await SessionService.updateSet(userId, setId, req.body);
            return sendSuccess(res, { updatedSet });
        } catch (error: any) {
            if (error.message === 'No active session found') {
                return sendError(res, error.message, 404);
            }
            logger.error('Error in updateSet', { error });
            return sendError(res, 'Server error', 500);
        }
    }

    static async patchSet(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const { setId } = req.params;
            const updatedSet = await SessionService.patchSet(userId, setId, req.body);
            return sendSuccess(res, { updatedSet });
        } catch (error: any) {
            if (error.message === 'No active session found') {
                return sendError(res, error.message, 404);
            }
            logger.error('Error in patchSet', { error });
            return sendError(res, 'Server error', 500);
        }
    }

    static async deleteSet(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const { setId } = req.params;
            await SessionService.deleteSet(userId, setId);
            return sendSuccess(res, null);
        } catch (error: any) {
            if (error.message === 'No active session found') {
                return sendError(res, error.message, 404);
            }
            logger.error('Error in deleteSet', { error });
            return sendError(res, 'Server error', 500);
        }
    }

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

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