module.exports = {
  async up(queryInterface, Sequelize) {
    const transaction = await queryInterface.sequelize.transaction();
    try {
      await queryInterface.createTable(
        'projects',
        {
          id: {
            type: Sequelize.DataTypes.UUID,
            defaultValue: Sequelize.DataTypes.UUIDV4,
            primaryKey: true,
          },
          name: {
            type: Sequelize.DataTypes.TEXT,
          },
          description: {
            type: Sequelize.DataTypes.TEXT,
          },
          status: {
            type: Sequelize.DataTypes.ENUM,
            values: ['active', 'completed', 'archived'],
          },
          startDate: {
            type: Sequelize.DataTypes.DATE,
          },
          endDate: {
            type: Sequelize.DataTypes.DATE,
          },
          importHash: {
            type: Sequelize.DataTypes.STRING(255),
            allowNull: true,
            unique: true,
          },
          tenantId: {
            type: Sequelize.DataTypes.UUID,
            references: {
              model: 'tenants',
              key: 'id',
            },
          },
          organizationsId: {
            type: Sequelize.DataTypes.UUID,
            references: {
              model: 'organizations',
              key: 'id',
            },
          },
          createdById: {
            type: Sequelize.DataTypes.UUID,
            references: {
              model: 'users',
              key: 'id',
            },
          },
          updatedById: {
            type: Sequelize.DataTypes.UUID,
            references: {
              model: 'users',
              key: 'id',
            },
          },
          createdAt: { type: Sequelize.DataTypes.DATE },
          updatedAt: { type: Sequelize.DataTypes.DATE },
          deletedAt: { type: Sequelize.DataTypes.DATE },
        },
        { transaction }
      );

      await queryInterface.createTable(
        'projects_trials',
        {
          id: {
            type: Sequelize.DataTypes.UUID,
            defaultValue: Sequelize.DataTypes.UUIDV4,
            primaryKey: true,
          },
          projectId: {
            type: Sequelize.DataTypes.UUID,
            references: {
              model: 'projects',
              key: 'id',
            },
            onDelete: 'CASCADE',
            onUpdate: 'CASCADE',
          },
          trialId: {
            type: Sequelize.DataTypes.UUID,
            references: {
              model: 'trials',
              key: 'id',
            },
            onDelete: 'CASCADE',
            onUpdate: 'CASCADE',
          },
          createdAt: { type: Sequelize.DataTypes.DATE },
          updatedAt: { type: Sequelize.DataTypes.DATE },
        },
        { transaction }
      );

      await transaction.commit();
    } catch (err) {
      await transaction.rollback();
      throw err;
    }
  },

  async down(queryInterface, Sequelize) {
    const transaction = await queryInterface.sequelize.transaction();
    try {
      await queryInterface.dropTable('projects_trials', { transaction });
      await queryInterface.dropTable('projects', { transaction });
      await transaction.commit();
    } catch (err) {
      await transaction.rollback();
      throw err;
    }
  },
};