const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    const Notification = sequelize.define('notification', {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        creation: { type: DataTypes.DATE, allowNull: false },
        modified: { type: DataTypes.DATE, allowNull: false },
        modified_by: { type: DataTypes.INTEGER, allowNull: false },
        owner: { type: DataTypes.INTEGER, allowNull: false },

        recipient_emails: { type: DataTypes.JSON, allowNull: true },
        notification_type: { type: DataTypes.STRING(150), allowNull: false },
        is_sent: { type: DataTypes.BOOLEAN, allowNull: true },
        is_read: { type: DataTypes.BOOLEAN, allowNull: false },
        sentAt: { type: DataTypes.DATE, allowNull: true },

        // fk
        workflow_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'workflows',
                key: 'id'
            },
            allowNull: false
        },
        employee_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'employees',
                key: 'id'
            },
            allowNull: false
        },
    });

    /**
     * Associates the Notification model with other models.
     * @param {object} models - The models to associate with.
     * @description
     *   - A Notification belongs to a Workflow.
     *   - A Notification belongs to an Employee (recipient).
     */
    Notification.associate = (models) => {
        /**
         * Associates the Notification model with the Workflow model.
         * Defines a many-to-one relationship between Notification and Workflow.
         * @param {object} models - The models to associate with.
         */
        Notification.belongsTo(models.Workflow, { foreignKey: 'workflow_id', as: 'workflow' });

        /**
         * Associates the Notification model with the Employee model.
         * Defines a many-to-one relationship between Notification and Employee (recipient).
         * @param {object} models - The models to associate with.
         */
        Notification.belongsTo(models.Employee, { foreignKey: 'employee_id', as: 'recipient' });
    };

    return Notification;
};
