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

module.exports = (sequelize) => {
	const EmailGroup = sequelize.define(
		"emailgroup",
		{
			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, defaultValue: true },

			name: { type: DataTypes.STRING(150), allowNull: false },
			display_name: { type: DataTypes.STRING(150), allowNull: false },
			description: { type: DataTypes.TEXT, allowNull: true },
			is_aggregate: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false },
			aggregate_of_group_ids: { type: DataTypes.JSON, allowNull: true },

			company_id: { type: DataTypes.INTEGER, allowNull: true },
		},
		{
			tableName: "email_groups",
			freezeTableName: true,
		}
	);

	EmailGroup.associate = (models) => {
		EmailGroup.belongsTo(models.Company, { as: "company", foreignKey: "company_id" });
		EmailGroup.hasMany(models.EmailGroupMember, { as: "emailgroupmember", foreignKey: "email_group_id" });
	};

	return EmailGroup;
};
