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

module.exports = (sequelize) => {
    const UserAnswer = sequelize.define(
        "useranswer",
        {
            id: {
                type: DataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true,
            },
            option: { type: DataTypes.STRING(10), allowNull: true, },
            is_correct: { type: DataTypes.BOOLEAN, allowNull: true },
            correct_point: { type: DataTypes.INTEGER, allowNull: true },
            type: { type: DataTypes.STRING, allowNull: true },

            // fk
            questionbank_id: { type: DataTypes.INTEGER, allowNull: false, },
            question_id: { type: DataTypes.INTEGER, allowNull: false, },
            user_id: { type: DataTypes.INTEGER, allowNull: false, }
        },
        {
            tableName: "useranswers",
            freezeTableName: true,
        }
    );

    /**
     * Associates the UserAnswer model with other models.
     * @param {object} models - The models to associate with.
     */
    UserAnswer.associate = (models) => {
        /**
         * Associates the UserAnswer model with the QuestionBank model.
         * Defines a many-to-one relationship between UserAnswer and QuestionBank.
         * @param {object} models - The models to associate with.
         */
        UserAnswer.belongsTo(models.QuestionBank, { foreignKey: "questionbank_id", as: "questionbank" });
        
        /**
         * Associates the UserAnswer model with the Question model.
         * Defines a many-to-one relationship between UserAnswer and Question.
         * @param {object} models - The models to associate with.
         */
        UserAnswer.belongsTo(models.Question, { foreignKey: "question_id", as: "question" });

        /**
         * Associates the UserAnswer model with the User model.
         * Defines a many-to-one relationship between UserAnswer and User.
         * @param {object} models - The models to associate with.
         */
        UserAnswer.belongsTo(models.User, { foreignKey: "user_id", as: "user" });
    };

    return UserAnswer;
};

