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

module.exports = (sequelize) => {
    const Comment = sequelize.define(
        "comment",
        {
            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: true },
            owner: { type: DataTypes.INTEGER, allowNull: true },

            doctype_id: { type: DataTypes.INTEGER, allowNull: false },
            doc_id: { type: DataTypes.INTEGER, allowNull: false },
            reply_id: { type: DataTypes.INTEGER, allowNull: true },
            comment: { type: DataTypes.TEXT, allowNull: false },
            edited: { type: DataTypes.BOOLEAN, allowNull: false },
        },
        {
            tableName: "comments",
            freezeTableName: true,
        }
    );

    Comment.associate = (models) => {


        /**
         * A Comment belongs to one Doctype.
         * @param {object} models - The models to associate with.
         * @description
         *   - A Comment belongs to one Doctype.
         *   - A Doctype has many Comments.
         */
        Comment.belongsTo(models.DocType, { foreignKey: 'doctype_id', as: 'doctype' });
        Comment.belongsTo(models.User, { foreignKey: 'owner', as: 'user' });
    };

    return Comment;
};
