import { Request, Response } from 'express';
import * as notificationService from '../services/notificationService';
import { sendResponse, handleError } from '../utils/apiResponse';

export const getMyNotifications = async (req: Request, res: Response): Promise<void> => {
  try {
    const page = Number(req.query.page ?? 1);
    const limit = Number(req.query.limit ?? 10);

    const { notifications, unreadCount, pagination } = await notificationService.getUserNotifications(
      req.user!.id,
      page,
      limit,
    );

    // notifications already include route/meta from service
    sendResponse(res, 200, 'Notifications fetched', { notifications, unreadCount }, pagination);
  } catch (error) {
    handleError(res, error);
  }
};

export const markNotificationRead = async (req: Request, res: Response): Promise<void> => {
  try {
    const notification = await notificationService.markNotificationRead(req.user!.id, req.params.id as string);
    // notification now includes route/meta
    sendResponse(res, 200, 'Notification marked as read', { notification });
  } catch (error) {
    handleError(res, error);
  }
};

export const markAllNotificationsRead = async (req: Request, res: Response): Promise<void> => {
  try {
    const result = await notificationService.markAllNotificationsRead(req.user!.id);
    sendResponse(res, 200, 'All notifications marked as read', result);
  } catch (error) {
    handleError(res, error);
  }
};
