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

module.exports = (sequelize) => {
    const QuestionBank = sequelize.define(
        "questionbank",
        {
            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 },

            name: { type: DataTypes.STRING(100), allowNull: false },
            display_name: { type: DataTypes.STRING(150), allowNull: true },
            description: { type: DataTypes.TEXT, allowNull: true },
            type: { type: DataTypes.STRING(50), allowNull: false },
        },
        {
            tableName: "questionbanks",
            freezeTableName: true,
        }
    );

    /**
     * Associates the QuestionBank model with other models.
     * @param {object} models - The models to associate with.
     */
    QuestionBank.associate = (models) => {
        /**
         * A QuestionBank has many Questions.
         * Defines a one-to-many relationship between QuestionBank and Question.
         * @param {object} models - The models to associate with.
         */
        QuestionBank.hasMany(models.Question, { foreignKey: "questionbank_id", as: "questions" });

        /**
         * A QuestionBank has many CourseQuestionBanks.
         * Defines a one-to-many relationship between QuestionBank and CourseQuestionBank.
         * @param {object} models - The models to associate with.
         */
        QuestionBank.hasMany(models.CourseQuestionBank, { foreignKey: "questionbank_id", as: "coursequestionbanks" });

        /**
         * A QuestionBank has many UserAnswers.
         * Defines a one-to-many relationship between QuestionBank and UserAnswer.
         * @param {object} models - The models to associate with.
         */
        QuestionBank.hasMany(models.UserAnswer, { foreignKey: "questionbank_id", as: "useranswers" });

        /**
         * A QuestionBank has many QuestionBankReviews.
         * Defines a one-to-many relationship between QuestionBank and QuestionBankReview.
         * @param {object} models - The models to associate with.
         */
        QuestionBank.hasMany(models.QuestionBankReview, { foreignKey: "questionbank_id", as: "questionbankreviews" });
    };

    return QuestionBank;
};
