const { UUID, UUIDV4, TEXT, BOOLEAN, DATE } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
	const ConcernMessages = sequelize.define(
		'ConcernMessages',
		{
			id: {
				type: UUID,
				defaultValue: UUIDV4,
				allowNull: false,
				primaryKey: true,
			},
			concernId: {
				type: UUID,
				allowNull: false,
			},
			message: {
				type: TEXT,
				allowNull: false,
			},
			member_id: {
				type: UUID,
				allowNull: true,
			},
			from_member: {
				type: BOOLEAN,
				allowNull: false,
				defaultValue: true,
			},
			createdAt: {
				type: DATE,
				allowNull: false,
			},
		},
		{
			tableName: 'concern_messages',
			updatedAt: false,
		},
	);

	ConcernMessages.associate = (models) => {
		ConcernMessages.belongsTo(models.Concerns, {
			foreignKey: 'concernId',
			as: 'concern',
			onDelete: 'CASCADE',
		});

		ConcernMessages.belongsTo(models.Members, {
			foreignKey: 'member_id',
			as: 'member',
		});
	};

	return ConcernMessages;
};
