'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
	async up(queryInterface, Sequelize) {
		return queryInterface.sequelize.transaction(async (transaction) => {
			await queryInterface.dropTable('driver_routes', { transaction });
			await queryInterface.dropTable('bus_schedules', { transaction });

			await queryInterface.createTable(
				'bus_schedules',
				{
					id: {
						type: Sequelize.UUID,
						defaultValue: Sequelize.UUIDV4,
						allowNull: false,
						primaryKey: true,
					},
					route_id: {
						type: Sequelize.UUID,
						allowNull: false,
					},
					driver_id: {
						type: Sequelize.UUID,
						allowNull: true,
					},
					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,
					},
					updatedAt: {
						type: Sequelize.DATE,
						allowNull: false,
					},
				},
				{ transaction },
			);
		});
	},

	async down(queryInterface, Sequelize) {
		return queryInterface.sequelize.transaction(async (transaction) => {
			await queryInterface.dropTable('bus_schedules', { transaction });

			await queryInterface.createTable(
				'bus_schedules',
				{
					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,
					},
					date: {
						type: Sequelize.DATEONLY,
						allowNull: false,
					},
					times: {
						type: Sequelize.JSON,
						allowNull: false,
					},
					createdAt: {
						type: Sequelize.DATE,
						allowNull: false,
					},
					updatedAt: {
						type: Sequelize.DATE,
						allowNull: false,
					},
				},
				{ transaction },
			);

			await queryInterface.createTable(
				'driver_routes',
				{
					id: {
						type: Sequelize.UUID,
						defaultValue: Sequelize.UUIDV4,
						primaryKey: true,
						allowNull: false,
					},
					route_id: {
						type: Sequelize.UUID,
						allowNull: false,
					},
					driver_id: {
						type: Sequelize.UUID,
						allowNull: true,
					},
					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,
					},
					updatedAt: {
						type: Sequelize.DATE,
						allowNull: false,
					},
				},
				{ transaction },
			);
		});
	},
};
