'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
	async up(queryInterface, Sequelize) {
		await queryInterface.sequelize.transaction(async (transaction) => {
			await queryInterface.createTable(
				'routes',
				{
					id: {
						type: Sequelize.UUID,
						defaultValue: Sequelize.UUIDV4,
						allowNull: false,
						primaryKey: true,
					},
					from: {
						type: Sequelize.STRING,
						allowNull: false,
					},
					to: {
						type: Sequelize.STRING,
						allowNull: false,
					},
					direction: {
						type: Sequelize.STRING,
						allowNull: false,
					},
					createdAt: {
						type: Sequelize.DATE,
						allowNull: false,
						defaultValue: Sequelize.NOW,
					},
					updatedAt: {
						type: Sequelize.DATE,
						allowNull: false,
						defaultValue: Sequelize.NOW,
					},
				},
				{ transaction },
			);

			await queryInterface.createTable('route_schedules', {
				id: {
					type: Sequelize.UUID,
					defaultValue: Sequelize.UUIDV4,
					allowNull: false,
					primaryKey: true,
				},
				route_id: {
					type: Sequelize.UUID,
					allowNull: false,
				},
				start_time: {
					type: Sequelize.TIME,
					allowNull: true,
				},
				end_time: {
					type: Sequelize.TIME,
					allowNull: true,
				},
				createdAt: {
					type: Sequelize.DATE,
					allowNull: false,
					defaultValue: Sequelize.NOW,
				},
				updatedAt: {
					type: Sequelize.DATE,
					allowNull: false,
					defaultValue: Sequelize.NOW,
				},
			});

			await queryInterface.createTable('driver_routes', {
				id: {
					type: Sequelize.UUID,
					defaultValue: Sequelize.UUIDV4,
					allowNull: false,
					primaryKey: true,
				},
				route_id: {
					type: Sequelize.UUID,
					allowNull: false,
				},
				driver_id: {
					type: Sequelize.UUID,
					allowNull: false,
				},
				route_schedule_id: {
					type: Sequelize.UUID,
					allowNull: false,
				},
				date: {
					type: Sequelize.DATEONLY,
					allowNull: false,
				},
				start_time: {
					type: Sequelize.TIME,
					allowNull: true,
				},
				start_status: {
					type: Sequelize.ENUM('On Time', 'Late', 'Early'),
					allowNull: true,
				},
				finish_time: {
					type: Sequelize.TIME,
					allowNull: true,
				},
				finish_status: {
					type: Sequelize.ENUM('On Time', 'Late', 'Early'),
					allowNull: true,
				},
				createdAt: {
					type: Sequelize.DATE,
					allowNull: false,
					defaultValue: Sequelize.NOW,
				},
				updatedAt: {
					type: Sequelize.DATE,
					allowNull: false,
					defaultValue: Sequelize.NOW,
				},
			});
		});
	},

	async down(queryInterface, Sequelize) {
		await queryInterface.sequelize.transaction(async (transaction) => {
			await queryInterface.dropTable('driver_routes', { transaction });
			await queryInterface.dropTable('route_schedules', { transaction });
			await queryInterface.dropTable('routes', { transaction });
		});
	},
};
