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

module.exports = (sequelize, DataTypes) => {
	const Posts = sequelize.define(
		'Posts',
		{
			id: {
				type: UUID,
				defaultValue: UUIDV4,
				primaryKey: true,
				allowNull: false,
			},
			member_id: {
				type: UUID,
				allowNull: false,
			},
			content: {
				type: TEXT,
				allowNull: true,
			},
			location: {
				type: STRING,
				allowNull: true,
			},
			is_shared: {
				type: BOOLEAN,
				allowNull: false,
				defaultValue: false,
			},
			event: {
				type: STRING,
				allowNull: true,
			},
			original_post_id: {
				type: UUID,
				allowNull: true,
				references: {
					model: 'posts',
					key: 'id',
				},
			},
		},
		{
			tableName: 'posts',
			timestamps: true,
		},
	);

	Posts.associate = (models) => {
		Posts.belongsTo(models.Members, { as: 'member', foreignKey: 'member_id' });
		Posts.hasMany(models.PostImages, { as: 'images', foreignKey: 'post_id' });
		Posts.hasMany(models.PostTags, { as: 'tags', foreignKey: 'post_id' });
		Posts.belongsTo(models.Posts, {
			as: 'originalPost',
			foreignKey: 'original_post_id',
		});
	};

	return Posts;
};
