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

module.exports = (sequelize) => {
	const Restaurants = sequelize.define(
		'Restaurants',
		{
			id: {
				type: DataTypes.UUID,
				defaultValue: DataTypes.UUIDV4,
				primaryKey: true,
				allowNull: false,
			},
			name: {
				type: DataTypes.STRING,
				allowNull: false,
			},
			address: {
				type: DataTypes.STRING,
				allowNull: false,
			},
			price: {
				type: DataTypes.DECIMAL(10, 2),
				allowNull: false,
				defaultValue: 0.0,
			},
			about: {
				type: DataTypes.TEXT('long'),
				allowNull: false,
			},
			features: {
				type: DataTypes.JSON,
				allowNull: true,
			},
			location: {
				type: DataTypes.TEXT,
				allowNull: true,
			},
			parking: {
				type: DataTypes.STRING,
				allowNull: true,
			},
			latitude: {
				type: DataTypes.DECIMAL(10, 8),
				allowNull: true,
			},
			longitude: {
				type: DataTypes.DECIMAL(11, 8),
				allowNull: true,
			},
			call: {
				type: DataTypes.STRING,
				allowNull: true,
			},
			website: {
				type: DataTypes.STRING,
				allowNull: true,
			},
			email: {
				type: DataTypes.STRING,
				allowNull: true,
			},
			createdAt: {
				type: DataTypes.DATE,
				defaultValue: DataTypes.NOW,
			},
			updatedAt: {
				type: DataTypes.DATE,
				defaultValue: DataTypes.NOW,
			},
		},
		{
			tableName: 'restaurants',
			timestamps: true,
		},
	);

	Restaurants.associate = (models) => {
		Restaurants.hasMany(models.RestaurantHours, {
			foreignKey: 'restaurant_id',
			as: 'hours',
		});
		Restaurants.hasMany(models.RestaurantImages, {
			foreignKey: 'restaurant_id',
			as: 'images',
		});
		Restaurants.hasMany(models.RestaurantSaves, {
			foreignKey: 'restaurant_id',
			as: 'saves',
		});
		Restaurants.hasMany(models.RestaurantReviews, {
			foreignKey: 'restaurant_id',
			as: 'reviews',
		});
		Restaurants.hasOne(models.RestaurantReviews, {
			foreignKey: 'restaurant_id',
			as: 'review',
		});
	};

	return Restaurants;
};
