import { Request, Response } from 'express';
import catchAsync from '@/shared/utils/catchAsync';
import { pick } from '@/shared/utils';
import * as pushNotificationService from './pushNotification.service';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { ApiError } from '@/shared/utils/errors';

const { NotificationResponseCodes } = responseCodes;

/**
 * GET /push-notifications
 * Fetch paginated push notifications (filtered by receivedTo, search, etc.)
 */
export const getPushNotifications = catchAsync(
  async (req: Request, res: Response) => {
    const filter = {
      ...pick(req.query, ['search', 'read']),
      receivedTo: req.user.id,
    };

    const options = pick(req.query, [
      'sortBy',
      'limit',
      'page',
      'fields',
      'populate',
      'includeTimeStamps',
    ]);

    const result = await pushNotificationService.queryPushNotifications(filter, options, req.user.id);

    res.success(
      result,
      NotificationResponseCodes.SUCCESS,
      'Push notifications fetched successfully',
    );
  },
);


export const updatePushNotification = catchAsync(
  async (req: Request, res: Response) => {
    const { id } = pick(req.params, ['id']);
    await pushNotificationService.updatePushNotification(id, req.body);

    res.success(
      true,
      NotificationResponseCodes.SUCCESS,
      'Push notification updated successfully',
    );
  },
);


// read all push notifications in bulk
export const bulkUpdatePushNotifications = catchAsync(
  async (req: Request, res: Response) => {
    const upatedCount = await pushNotificationService.bulkUpdatePushNotificationsByIds(
      req.user.id,
      req.body,
    );

    res.success(
      upatedCount ,
      NotificationResponseCodes.SUCCESS,
      'Push notifications updated successfully',
    );
  },
);
