import { Request, Response } from "express";
import { isValidObjectId } from "mongoose";
import Notification from "../../models/notification";

const getNotifications = async (req: Request, res: Response) => {
  const { page = 1, perPage = 10, isRead } = req.query;
  const { id } = req.params;

  if (!isValidObjectId(id)) {
    return res.status(400).json({
      message: "User ID is not valid",
    });
  }
  console.log("/notifications");
  try {
    const filter = {
      user: id,
    } as any;

    if (isRead) {
      filter["isRead"] = isRead;
    }

    const [notifications, totalItemCount] = await Promise.all([
      Notification.find({
        user: id,
      })
        .sort({ createdAt: -1 })
        .skip((Number(page) - 1) * Number(perPage))
        .limit(Number(perPage))
        .populate("occasion", "key title icon"),
      Notification.countDocuments({ user: id }),
    ]);

    res.status(200).json({ notifications, totalItemCount });
  } catch (err) {
    res.status(500).json({ message: err });
  }
};

module.exports = getNotifications;
