import { Request, Response, NextFunction } from 'express';
import * as notificationsService from '../services/notifications.service';
import { sendSuccess, sendError } from '../utils/apiResponse';

export const list = async (req: Request, res: Response) => {
  try {
    const notifications = await notificationsService.getUserNotifications(req.user!.id);
    return sendSuccess(res, 200, 'Notifications', notifications);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const unreadCount = async (req: Request, res: Response) => {
  try {
    const count = await notificationsService.getUnreadCount(req.user!.id);
    return sendSuccess(res, 200, 'Compteur de notifications', { count });
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const markAsRead = async (req: Request, res: Response) => {
  try {
    await notificationsService.markAsRead(parseInt(req.params.id as string), req.user!.id);
    return sendSuccess(res, 200, 'Notification marquée comme lue');
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const markAllAsRead = async (req: Request, res: Response) => {
  try {
    await notificationsService.markAllAsRead(req.user!.id);
    return sendSuccess(res, 200, 'Toutes les notifications marquées comme lues');
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const deleteNotification = async (req: Request, res: Response) => {
  try {
    // Ideally add notificationsService.deleteNotification, but let's implement it.
    await notificationsService.deleteNotification(parseInt(req.params.id as string), req.user!.id);
    return sendSuccess(res, 200, 'Notification supprimée');
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};
