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

module.exports = (sequelize) => {
	const HotelReviews = sequelize.define(
		'HotelReviews',
		{
			id: {
				type: DataTypes.UUID,
				defaultValue: DataTypes.UUIDV4,
				primaryKey: true,
				allowNull: false,
			},
			hotel_id: {
				type: DataTypes.UUID,
				allowNull: false,
				references: {
					model: 'hotels',
					key: 'id',
				},
			},
			hotel_room_id: {
				type: DataTypes.UUID,
				allowNull: true,
				references: {
					model: 'hotel_rooms',
					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,
			},
			booking_id: {
				type: DataTypes.UUID,
				allowNull: true,
				onDelete: 'CASCADE',
			},
			is_anonymous: {
				type: DataTypes.BOOLEAN,
				allowNull: false,
				defaultValue: false,
			},
		},
		{
			tableName: 'hotel_reviews',
			timestamps: true,
		},
	);

	HotelReviews.associate = (models) => {
		HotelReviews.belongsTo(models.Hotels, {
			foreignKey: 'hotel_id',
			as: 'hotel',
		});

		HotelReviews.belongsTo(models.HotelRooms, {
			foreignKey: 'hotel_room_id',
			as: 'room',
		});

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

		HotelReviews.hasMany(models.HotelReviewImages, {
			foreignKey: 'hotel_review_id',
			as: 'images',
			onDelete: 'CASCADE',
		});
	};

	return HotelReviews;
};
