FATAL EXCEPTION: OkHttp Dispatcher
                                                                                                    Process: com.example.neurea, PID: 8385
                                                                                                    java.lang.IllegalStateException: cannot make a new request because the previous response is still open: please call response.close()
                                                                                                    	at okhttp3.internal.connection.RealCall.enterNetworkInterceptorExchange(RealCall.kt:229)
                                                                                                    	at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:66)
                                                                                                    	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
                                                                                                    	at com.example.neurea.api.AuthInterceptor.intercept(AuthInterceptor.kt:41)
                                                                                                    	at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
                                                                                                    	at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
                                                                                                    	at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:517)
                                                                                                    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
                                                                                                    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
                                                                                                    	at java.lang.Thread.run(Thread.java:1012)
class AuthInterceptor(private val authManager: AuthManager) : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        var originalRequest = chain.request()

        val accessToken = authManager.getAccessToken()

        if (accessToken != null) {
            originalRequest = originalRequest.newBuilder()
                .header("Authorization", "Bearer $accessToken")
                .build()
        }

        var response = chain.proceed(originalRequest)

        if (response.code == 401) {
            val refreshToken = authManager.getRefreshToken()

            if (refreshToken != null) {
                val newTokensResponse = refreshAccessToken(refreshToken)

                if (newTokensResponse.isSuccessful && newTokensResponse.body() != null) {
                    val newAccessToken = newTokensResponse.body()!!.access
                    authManager.saveTokens(newAccessToken, refreshToken)

                    originalRequest = originalRequest.newBuilder()
                        .header("Authorization", "Bearer $newAccessToken")
                        .build()

                    response = chain.proceed(originalRequest)
                }
            }
        }

        return response
    }

    private fun refreshAccessToken(refreshToken: String): retrofit2.Response<TokenResponse> {
        val apiService = RetrofitClient.getInstance(authManager.getContext()).create(Api::class.java)
        val refreshRequest = hashMapOf("refresh" to refreshToken)

        return apiService.refreshToken(refreshRequest).execute()
    }
}