const db = require('../config/database');

const Notification = {
    // Get all notifications for a user
    findByUserId: (userId, callback) => {
        const query = 'SELECT * FROM notifications WHERE user_id = ? ORDER BY id DESC';
        db.query(query, [userId], callback);
    },

    // Get notification by ID
    findById: (notificationId, callback) => {
        const query = 'SELECT * FROM notifications WHERE id = ?';
        db.query(query, [notificationId], callback);
    },

    // Create new notification
    create: (notificationData, callback) => {
        const { user_id, text, request_id = null } = notificationData;
        const query = 'INSERT INTO notifications (user_id, request_id, text) VALUES (?, ?, ?)';
        db.query(query, [user_id, request_id, text], callback);
    },

    // Mark a notification as read for a specific user
    markAsRead: (userId, notificationId, callback) => {
        const query =
            'UPDATE notifications SET is_read = TRUE WHERE id = ? AND user_id = ?';
        db.query(query, [notificationId, userId], callback);
    },

    // Mark all notifications as read for a user
    markAllAsRead: (userId, callback) => {
        const query =
            'UPDATE notifications SET is_read = TRUE WHERE user_id = ? AND is_read = FALSE';
        db.query(query, [userId], callback);
    },

    // Delete all notifications for a user
    deleteAllByUserId: (userId, callback) => {
        const query = 'DELETE FROM notifications WHERE user_id = ?';
        db.query(query, [userId], callback);
    }
};

module.exports = Notification;



