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

module.exports = (sequelize) => {
    const Course = sequelize.define('course', {
        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 },
        enabled: { type: DataTypes.BOOLEAN, allowNull: false },

        title: { type: DataTypes.STRING, allowNull: false },
        description: { type: DataTypes.TEXT, allowNull: true },
        url: { type: DataTypes.TEXT, allowNull: true },
        icon: { type: DataTypes.TEXT, allowNull: true },
    },  {
        tableName: 'courses',
        freezeTableName: true
    });

    /**
     * Associates the Course model with other models.
     * @param {object} models - The models to associate with.
     */
    Course.associate = (models) => {
        /**
         * A Course has many CourseChild.
         * Defines a one-to-many relationship between Course and CourseChild.
         * @param {object} models - The models to associate with.
         */
        Course.hasMany(models.CourseChild, { foreignKey: 'course_id', as: 'coursechilds' });

        /**
         * A Course has many CourseImage.
         * Defines a one-to-many relationship between Course and CourseImage.
         * @param {object} models - The models to associate with.
         */
        Course.belongsToMany(models.Image, { through: models.CourseImage, foreignKey: 'course_id', otherKey: 'image_id', as: 'images' });

        /**
         * A Course has many CourseDocument.
         * Defines a one-to-many relationship between Course and CourseDocument.
         * @param {object} models - The models to associate with.
         */
        Course.belongsToMany(models.Document, { through: models.CourseDocument, foreignKey: 'course_id', otherKey: 'document_id', as: 'documents' });

        /**
         * A Course has many CourseVideo.
         * Defines a one-to-many relationship between Course and CourseVideo.
         * @param {object} models - The models to associate with.
         */
        Course.belongsToMany(models.Video, { through: models.CourseVideo, foreignKey: 'course_id', otherKey: 'video_id', as: 'videos' });

        /**
         * A Course has many ProjectCourse.
         * Defines a one-to-many relationship between Course and ProjectCourse.
         * @param {object} models - The models to associate with.
         */
        Course.belongsToMany(models.Project, { through: models.ProjectCourse, foreignKey: 'course_id', otherKey: 'project_id', as: 'projects' });

        /**
         * A Course has many CourseQuestionBank.
         * Defines a one-to-many relationship between Course and CourseQuestionBank.
         * @param {object} models - The models to associate with.
         */
        Course.belongsToMany(models.QuestionBank, { through: models.CourseQuestionBank, foreignKey: 'course_id', otherKey: 'questionbank_id', as: 'questionbanks' });

        /**
         * A Course has many CourseDevelopment.
         * Defines a one-to-many relationship between Course and CourseDevelopment.
         * @param {object} models - The models to associate with.
         */
        Course.belongsToMany(models.Development, { through: models.CourseDevelopment, foreignKey: 'course_id', otherKey: 'development_id', as: 'developments' });

        Course.hasMany(models.Progression, { foreignKey: 'course_id', as: 'progressions' });
    };

    return Course;
};
