import { Request, Response } from 'express';
import * as notificationService from './notification.service';
import catchAsync from '@/shared/utils/catchAsync';
import responseCodes from '@/shared/utils/responseCode/responseCode';
import { pick } from '@/shared/utils';

const { NotificationResponseCodes } = responseCodes;

export const createNotification = catchAsync(
  async (req: Request, res: Response) => {
    const created = await notificationService.createNotification({
      ...req.body,
    });

    res.success(
      created,
      NotificationResponseCodes.SUCCESS,
      'Notification created successfully',
    );
  },
);

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

    res.success(
      updated,
      NotificationResponseCodes.SUCCESS,
      'Notification updated successfully',
    );
  },
);

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

    res.success(
      notification,
      NotificationResponseCodes.SUCCESS,
      'Notification fetched successfully',
    );
  },
);

export const getNotifications = catchAsync(
  async (req: Request, res: Response) => {
    const filter = pick(req.query, [
      'search',
      'company',
      'status',
      'userType',
      'type',
    ]);
    const options = pick(req.query, [
      'sortBy',
      'limit',
      'page',
      'fields',
      'populate',
      'includeTimeStamps',
    ]);

    const result = await notificationService.queryNotifications(filter, options);

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

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

    res.success(
      null,
      NotificationResponseCodes.SUCCESS,
      'Notification deleted successfully',
    );
  },
);
