module.exports = {
  async up(queryInterface, Sequelize) {
    const transaction = await queryInterface.sequelize.transaction();

    try {
      const tables = await queryInterface.showAllTables({ transaction });
      const tableNames = tables.map((table) => (typeof table === 'object' ? table.tableName : table));

      if (!tableNames.includes('vorta_login_otps')) {
        await queryInterface.createTable(
          'vorta_login_otps',
          {
            id: {
              type: Sequelize.DataTypes.UUID,
              defaultValue: Sequelize.DataTypes.UUIDV4,
              primaryKey: true,
            },
            userId: {
              type: Sequelize.DataTypes.UUID,
              references: {
                model: 'users',
                key: 'id',
              },
            },
            organizationsId: {
              type: Sequelize.DataTypes.UUID,
              references: {
                model: 'organizations',
                key: 'id',
              },
            },
            phone: {
              type: Sequelize.DataTypes.STRING(40),
              allowNull: false,
            },
            otpHash: {
              type: Sequelize.DataTypes.STRING(255),
              allowNull: false,
            },
            status: {
              type: Sequelize.DataTypes.STRING(40),
              allowNull: false,
              defaultValue: 'pending',
            },
            attempts: {
              type: Sequelize.DataTypes.INTEGER,
              allowNull: false,
              defaultValue: 0,
            },
            expiresAt: {
              type: Sequelize.DataTypes.DATE,
              allowNull: false,
            },
            verifiedAt: {
              type: Sequelize.DataTypes.DATE,
            },
            importHash: {
              type: Sequelize.DataTypes.STRING(255),
              allowNull: true,
              unique: true,
            },
            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 transaction.commit();
    } catch (error) {
      await transaction.rollback();
      console.error('Failed to create vorta_login_otps table:', error);
      throw error;
    }
  },

  async down(queryInterface) {
    const transaction = await queryInterface.sequelize.transaction();

    try {
      const tables = await queryInterface.showAllTables({ transaction });
      const tableNames = tables.map((table) => (typeof table === 'object' ? table.tableName : table));

      if (tableNames.includes('vorta_login_otps')) {
        await queryInterface.dropTable('vorta_login_otps', { transaction });
      }

      await transaction.commit();
    } catch (error) {
      await transaction.rollback();
      console.error('Failed to drop vorta_login_otps table:', error);
      throw error;
    }
  },
};
