'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
	async up(queryInterface, Sequelize) {
		await queryInterface.createTable('products', {
			id: {
				type: Sequelize.UUID,
				defaultValue: Sequelize.UUIDV4,
				allowNull: false,
				primaryKey: true,
			},
			name: {
				type: Sequelize.STRING,
				allowNull: false,
			},
			description: {
				type: Sequelize.TEXT('long'),
				allowNull: true,
			},
			price: {
				type: Sequelize.DECIMAL(10, 2),
				allowNull: false,
			},
			colors: {
				type: Sequelize.JSON,
				allowNull: true,
			},
			sizes: {
				type: Sequelize.JSON,
				allowNull: true,
			},
			createdAt: {
				type: Sequelize.DATE,
				allowNull: false,
				defaultValue: Sequelize.NOW,
			},
		});

		await queryInterface.createTable('product_details', {
			id: {
				type: Sequelize.UUID,
				defaultValue: Sequelize.UUIDV4,
				allowNull: false,
				primaryKey: true,
			},
			product_id: {
				type: Sequelize.UUID,
				allowNull: false,
				references: {
					model: 'products',
					key: 'id',
				},
			},
			color: {
				type: Sequelize.STRING,
				allowNull: true,
			},
			size: {
				type: Sequelize.STRING,
				allowNull: true,
			},
			stock: {
				type: Sequelize.INTEGER,
				allowNull: false,
				defaultValue: 0,
			},
		});

		await queryInterface.createTable('product_images', {
			id: {
				type: Sequelize.UUID,
				defaultValue: Sequelize.UUIDV4,
				allowNull: false,
				primaryKey: true,
			},
			product_id: {
				type: Sequelize.UUID,
				allowNull: false,
				references: {
					model: 'products',
					key: 'id',
				},
			},
			image: {
				type: Sequelize.STRING,
				allowNull: false,
			},
			category: {
				type: Sequelize.STRING,
				allowNull: false,
			},
		});

		await queryInterface.createTable('product_carts', {
			id: {
				type: Sequelize.UUID,
				defaultValue: Sequelize.UUIDV4,
				allowNull: false,
				primaryKey: true,
			},
			product_id: {
				type: Sequelize.UUID,
				allowNull: false,
				references: {
					model: 'products',
					key: 'id',
				},
			},
			member_id: {
				type: Sequelize.UUID,
				allowNull: false,
				references: {
					model: 'members',
					key: 'id',
				},
			},
			color: {
				type: Sequelize.STRING,
				allowNull: true,
			},
			size: {
				type: Sequelize.STRING,
				allowNull: true,
			},
			quantity: {
				type: Sequelize.INTEGER,
				allowNull: false,
				defaultValue: 1,
			},
			updatedAt: {
				type: Sequelize.DATE,
				allowNull: false,
				defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
			},
		});
	},

	async down(queryInterface, Sequelize) {
		await queryInterface.dropTable('product_carts');
		await queryInterface.dropTable('product_images');
		await queryInterface.dropTable('product_details');
		await queryInterface.dropTable('products');
	},
};
