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

export class WeightController {
    static async getWeightHistory(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const weights = await WeightService.getWeightHistory(userId);
            return sendSuccess(res, weights);
        } catch (error) {
            logger.error('Error in getWeightHistory', { error });
            return sendError(res, 'Failed to fetch weight history', 500);
        }
    }

    static async logWeight(req: any, res: Response) {
        try {
            const userId = req.user.userId;
            const { weight, dateStr } = req.body;

            if (!weight || !dateStr) {
                return sendError(res, 'Weight and dateStr are required', 400);
            }

            const record = await WeightService.logWeight(userId, parseFloat(weight), dateStr);
            return sendSuccess(res, record);
        } catch (error) {
            logger.error('Error in logWeight', { error });
            return sendError(res, 'Failed to log weight', 500);
        }
    }
}
