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

module.exports = (sequelize) => {
	const HotelBookings = sequelize.define(
		'HotelBookings',
		{
			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: false,
				references: {
					model: 'hotel_rooms',
					key: 'id',
				},
			},
			reference_no: {
				type: DataTypes.STRING,
				allowNull: false,
			},
			member_id: {
				type: DataTypes.UUID,
				allowNull: false,
				references: {
					model: 'members',
					key: 'id',
				},
			},
			payment_status: {
				type: DataTypes.ENUM('Paid', 'Partially Paid', 'Unpaid'),
				allowNull: false,
				defaultValue: 'Unpaid',
			},
			balance: {
				type: DataTypes.DECIMAL(10, 2),
				allowNull: false,
				defaultValue: 0.0,
			},
			payment_method: {
				type: DataTypes.STRING,
				allowNull: false,
			},
			payment: {
				type: DataTypes.DECIMAL(10, 2),
				allowNull: false,
				defaultValue: 0.0,
			},
			check_in: {
				type: DataTypes.DATEONLY,
				allowNull: false,
			},
			check_out: {
				type: DataTypes.DATEONLY,
				allowNull: false,
			},
			price: {
				type: DataTypes.DECIMAL(10, 2),
				allowNull: false,
			},
			duration: {
				type: DataTypes.INTEGER,
				allowNull: false,
			},
			total: {
				type: DataTypes.DECIMAL(10, 2),
				allowNull: false,
			},
			status: {
				type: DataTypes.ENUM('Unpaid', 'Pending', 'Completed', 'Cancelled'),
				allowNull: false,
				defaultValue: 'Unpaid',
			},
		},
		{
			tableName: 'hotel_bookings',
			timestamps: true,
		},
	);

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

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

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

		HotelBookings.hasOne(models.HotelReviews, {
			as: 'review',
			foreignKey: 'booking_id',
		});
	};

	return HotelBookings;
};
