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

module.exports = (sequelize) => {
    const Level = sequelize.define('level', {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        level_group: { type: DataTypes.STRING, allowNull: false },
        level_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: 'levels',
    }, {
        freezeTableName: true
    });

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

    return Level;
};
