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

module.exports = (sequelize) => {
    const Factor = sequelize.define('factor', {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        factor_name: { type: DataTypes.STRING, allowNull: false },
        description: { type: DataTypes.TEXT, allowNull: false },

        // fk
        development_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'developments',
                key: 'id'
            },
            onDelete: 'CASCADE'
        },
    }, {
        indexes: [{ fields: ['development_id'] }]
    }, {
        tableName: 'factors',
    }, {
        freezeTableName: true
    });

    /**
     * Associates the Factor model with other models.
     * @param {object} models - The models to associate with.
     */
    Factor.associate = (models) => {
        /**
         * Associates the Factor model with the Development model.
         * Defines a many-to-one relationship between Factor and Development.
         * @param {object} models - The models to associate with.
         */
        Factor.belongsTo(models.Development, { foreignKey: 'development_id', as: 'development' });
    };

    return Factor;
};
