const notificationModel = require('../models/notification.model');

class NotificationService {
    async createNotification(data) {
        // If the new notification is active, de-activate others if we only want one active popup (optional logic)
        // For now, let's just create it.
        return await notificationModel.create(data);
    }

    async updateNotification(id, data) {
        return await notificationModel.findByIdAndUpdate(id, data, { new: true });
    }

    async deleteNotification(id) {
        return await notificationModel.findByIdAndDelete(id);
    }

    async getNotifications() {
        return await notificationModel.find().sort({ createdAt: -1 });
    }

    async getActiveNotification() {
        // Get the active notification with highest priority, then latest created
        return await notificationModel.findOne({ isActive: true }).sort({ priority: -1, createdAt: -1 });
    }

    async getNotificationById(id) {
        return await notificationModel.findById(id);
    }
}

module.exports = new NotificationService();
