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

module.exports = (sequelize) => {
	const RestaurantReviews = sequelize.define(
		'RestaurantReviews',
		{
			id: {
				type: DataTypes.UUID,
				defaultValue: DataTypes.UUIDV4,
				primaryKey: true,
				allowNull: false,
			},
			restaurant_id: {
				type: DataTypes.UUID,
				allowNull: false,
				references: {
					model: 'restaurants',
					key: 'id',
				},
			},
			member_id: {
				type: DataTypes.UUID,
				allowNull: false,
				references: {
					model: 'members',
					key: 'id',
				},
			},
			rating: {
				type: DataTypes.INTEGER,
				allowNull: false,
				validate: {
					min: 1,
					max: 5,
				},
			},
			review: {
				type: DataTypes.TEXT('long'),
				allowNull: true,
			},
			is_anonymous: {
				type: DataTypes.BOOLEAN,
				allowNull: false,
				defaultValue: false,
			},
			booking_id: {
				type: DataTypes.UUID,
				allowNull: true,
				onDelete: 'CASCADE',
			},
			createdAt: {
				type: DataTypes.DATE,
				defaultValue: DataTypes.NOW,
			},
		},
		{
			tableName: 'restaurant_reviews',
			timestamps: false,
		},
	);

	RestaurantReviews.associate = (models) => {
		RestaurantReviews.belongsTo(models.Restaurants, {
			foreignKey: 'restaurant_id',
			as: 'restaurant',
		});
		RestaurantReviews.belongsTo(models.Members, {
			foreignKey: 'member_id',
			as: 'member',
		});
		RestaurantReviews.hasMany(models.RestaurantReviewImages, {
			foreignKey: 'restaurant_review_id',
			as: 'images',
		});
	};

	return RestaurantReviews;
};
