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

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

            option: { type: DataTypes.STRING(10), allowNull: true, },
            text: { type: DataTypes.TEXT, allowNull: false },
            parameter: { type: DataTypes.TEXT, allowNull: true },
            is_correct: { type: DataTypes.BOOLEAN, allowNull: true },
            correct_point: { type: DataTypes.INTEGER, allowNull: true },

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

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

    return Answer;
};
