const Notification = require('../models/Notification');

const notificationController = {
    /**
     * Get all notifications
     */
    getAll: async (req, res) => {
        try {
            const { page, limit, search, startDate, endDate, filterType } = req.query;
            const result = await Notification.getAll(page, limit, search, startDate, endDate, filterType);
            res.json({
                success: true,
                ...result
            });
        } catch (error) {
            res.status(500).json({
                success: false,
                message: error.message
            });
        }
    },

    /**
     * Get notification by id
     */
    getById: async (req, res) => {
        try {
            const notification = await Notification.findById(req.params.id);
            if (!notification) {
                return res.status(404).json({
                    success: false,
                    message: 'Notification not found'
                });
            }
            res.json({
                success: true,
                data: notification
            });
        } catch (error) {
            res.status(500).json({
                success: false,
                message: error.message
            });
        }
    },

    /**
     * Create new notification
     */
    create: async (req, res) => {
        try {
            const id = await Notification.create(req.body);
            res.status(201).json({
                success: true,
                message: 'Notification created successfully',
                id
            });
        } catch (error) {
            res.status(500).json({
                success: false,
                message: error.message
            });
        }
    },

    /**
     * Update notification
     */
    update: async (req, res) => {
        try {
            const updated = await Notification.update(req.params.id, req.body);
            if (!updated) {
                return res.status(404).json({
                    success: false,
                    message: 'Notification not found or no changes made'
                });
            }
            res.json({
                success: true,
                message: 'Notification updated successfully'
            });
        } catch (error) {
            res.status(500).json({
                success: false,
                message: error.message
            });
        }
    },

    /**
     * Delete notification (soft delete)
     */
    delete: async (req, res) => {
        try {
            const deleted = await Notification.delete(req.params.id);
            if (!deleted) {
                return res.status(404).json({
                    success: false,
                    message: 'Notification not found'
                });
            }
            res.json({
                success: true,
                message: 'Notification deleted successfully'
            });
        } catch (error) {
            res.status(500).json({
                success: false,
                message: error.message
            });
        }
    },

    /**
     * Restore notification
     */
    restore: async (req, res) => {
        try {
            const restored = await Notification.restore(req.params.id);
            if (!restored) {
                return res.status(404).json({
                    success: false,
                    message: 'Notification not found'
                });
            }
            res.json({
                success: true,
                message: 'Notification restored successfully'
            });
        } catch (error) {
            res.status(500).json({
                success: false,
                message: error.message
            });
        }
    }
};

module.exports = notificationController;
