'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
	async up(queryInterface, Sequelize) {
		await queryInterface.createTable('restaurants', {
			id: {
				type: Sequelize.UUID,
				defaultValue: Sequelize.UUIDV4,
				primaryKey: true,
				allowNull: false,
			},
			name: {
				type: Sequelize.STRING,
				allowNull: false,
			},
			price: {
				type: Sequelize.DECIMAL(10, 2),
				allowNull: false,
				defaultValue: 0.0,
			},
			about: {
				type: Sequelize.TEXT('long'),
				allowNull: false,
			},
			features: {
				type: Sequelize.JSON,
				allowNull: true,
			},
			location: {
				type: Sequelize.TEXT,
				allowNull: true,
			},
			parking: {
				type: Sequelize.STRING,
				allowNull: true,
			},
			latitude: {
				type: Sequelize.DECIMAL(10, 8),
				allowNull: true,
			},
			longitude: {
				type: Sequelize.DECIMAL(11, 8),
				allowNull: true,
			},
			call: {
				type: Sequelize.STRING,
				allowNull: true,
			},
			website: {
				type: Sequelize.STRING,
				allowNull: true,
			},
			email: {
				type: Sequelize.STRING,
				allowNull: true,
			},
			createdAt: {
				type: Sequelize.DATE,
				allowNull: false,
				defaultValue: Sequelize.NOW,
			},
			updatedAt: {
				type: Sequelize.DATE,
				allowNull: false,
				defaultValue: Sequelize.NOW,
			},
		});

		await queryInterface.createTable('restaurant_hours', {
			id: {
				type: Sequelize.UUID,
				defaultValue: Sequelize.UUIDV4,
				primaryKey: true,
				allowNull: false,
			},
			restaurant_id: {
				type: Sequelize.UUID,
				allowNull: false,
				references: {
					model: 'restaurants',
					key: 'id',
				},
			},
			day_of_week: {
				type: Sequelize.INTEGER,
				allowNull: false,
			},
			open_time: {
				type: Sequelize.TIME,
				allowNull: true,
			},
			close_time: {
				type: Sequelize.TIME,
				allowNull: true,
			},
			is_closed: {
				type: Sequelize.BOOLEAN,
				allowNull: false,
				defaultValue: false,
			},
		});

		await queryInterface.createTable('restaurant_images', {
			id: {
				type: Sequelize.UUID,
				defaultValue: Sequelize.UUIDV4,
				primaryKey: true,
				allowNull: false,
			},
			restaurant_id: {
				type: Sequelize.UUID,
				allowNull: false,
				references: {
					model: 'restaurants',
					key: 'id',
				},
			},
			image: {
				type: Sequelize.STRING,
				allowNull: false,
			},
		});

		await queryInterface.createTable('restaurant_saves', {
			id: {
				type: Sequelize.UUID,
				defaultValue: Sequelize.UUIDV4,
				primaryKey: true,
				allowNull: false,
			},
			restaurant_id: {
				type: Sequelize.UUID,
				allowNull: false,
				references: {
					model: 'restaurants',
					key: 'id',
				},
			},
			member_id: {
				type: Sequelize.UUID,
				allowNull: false,
				references: {
					model: 'members',
					key: 'id',
				},
			},
			createdAt: {
				type: Sequelize.DATE,
				allowNull: false,
				defaultValue: Sequelize.NOW,
			},
		});

		await queryInterface.createTable('restaurant_reviews', {
			id: {
				type: Sequelize.UUID,
				defaultValue: Sequelize.UUIDV4,
				primaryKey: true,
				allowNull: false,
			},
			restaurant_id: {
				type: Sequelize.UUID,
				allowNull: false,
				references: {
					model: 'restaurants',
					key: 'id',
				},
			},
			member_id: {
				type: Sequelize.UUID,
				allowNull: false,
				references: {
					model: 'members',
					key: 'id',
				},
			},
			rating: {
				type: Sequelize.INTEGER,
				allowNull: false,
			},
			review: {
				type: Sequelize.TEXT('long'),
				allowNull: true,
			},
			createdAt: {
				type: Sequelize.DATE,
				allowNull: false,
				defaultValue: Sequelize.NOW,
			},
			updatedAt: {
				type: Sequelize.DATE,
				allowNull: false,
				defaultValue: Sequelize.NOW,
			},
		});

		await queryInterface.createTable('restaurant_review_images', {
			id: {
				type: Sequelize.UUID,
				defaultValue: Sequelize.UUIDV4,
				primaryKey: true,
				allowNull: false,
			},
			restaurant_review_id: {
				type: Sequelize.UUID,
				allowNull: false,
				references: {
					model: 'restaurant_reviews',
					key: 'id',
				},
			},
			image: {
				type: Sequelize.STRING,
				allowNull: false,
			},
		});
	},

	async down(queryInterface, Sequelize) {
		await queryInterface.dropTable('restaurant_review_images');
		await queryInterface.dropTable('restaurant_reviews');
		await queryInterface.dropTable('restaurant_saves');
		await queryInterface.dropTable('restaurant_images');
		await queryInterface.dropTable('restaurant_hours');
		await queryInterface.dropTable('restaurants');
	},
};
