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

module.exports = (sequelize) => {
    const CourseChild = sequelize.define('coursechild', {
        id: {
            type: DataTypes.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        enabled: { type: DataTypes.BOOLEAN, allowNull: false },

        title: { type: DataTypes.TEXT, allowNull: false },
        description: { type: DataTypes.TEXT, allowNull: true },
        other_description: { type: DataTypes.TEXT, allowNull: true },
        icon: { type: DataTypes.TEXT, allowNull: true },

        // fk
        course_id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: 'Courses',
                key: 'id'
            }
        },
    }, {
        tableName: 'coursechilds',
    }, {
        freezeTableName: true
    });

    /**
     * Associates the CourseChild model with other models.
     * @param {object} models - The models to associate with.
     * @description
     *   - A CourseChild belongs to one User.
     */
    CourseChild.associate = (models) => {
        /**
         * A CourseChild belongs to one Course.
         * @param {object} models - The models to associate with.
         */
        CourseChild.belongsTo(models.Course, { foreignKey: 'course_id', as: 'course' });
    };

    return CourseChild;
};
