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

module.exports = (sequelize) => {
    const CourseImage = sequelize.define('courseimage', {
        course_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'courses',
                key: 'id',
                primaryKey: true
            }
        },
        image_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'images',
                key: 'id',
                primaryKey: true
            }
        }
    }, {
        tableName: 'courseimages',
    }, {
        freezeTableName: true
    });

    /**
     * Associates the CourseImage model with other models.
     * @param {object} models - The models to associate with.
     */
    CourseImage.associate = (models) => {
        /**
         * Associate CourseImage with Course
         * @param {object} models - The models to associate with.
         * @property {object} course - The Course model associated with CourseImage.
         */
        CourseImage.belongsTo(models.Course, {
            foreignKey: 'course_id',
            as: 'course'
        });

        /**
         * Associate CourseImage with Image
         * @param {object} models - The models to associate with.
         * @property {object} image - The Image model associated with CourseImage.
         */
        CourseImage.belongsTo(models.Image, {
            foreignKey: 'image_id',
            as: 'image'
        });
    };

    return CourseImage;
};