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

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

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

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