/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.trino.plugin.bigquery;

import com.google.api.gax.rpc.FixedHeaderProvider;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.auth.Credentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.Scopes;
import com.google.inject.Singleton;
import io.trino.spi.NodeManager;

import java.util.Optional;

import static io.airlift.configuration.ConfigBinder.configBinder;

public class BigQueryConnectorModule
        implements Module
{
    @Provides
    @Singleton
    public static HeaderProvider createHeaderProvider(NodeManager nodeManager)
    {
        return FixedHeaderProvider.create("user-agent", "Trino/" + nodeManager.getCurrentNode().getVersion());
    }

    @Override
    public void configure(Binder binder)
    {
        // BigQuery related
        binder.bind(BigQueryStorageClientFactory.class).in(Scopes.SINGLETON);

        // Connector implementation
        binder.bind(BigQueryConnector.class).in(Scopes.SINGLETON);

        binder.bind(BigQueryMetadata.class).in(Scopes.SINGLETON);
        binder.bind(BigQuerySplitManager.class).in(Scopes.SINGLETON);
        binder.bind(BigQueryPageSourceProvider.class).in(Scopes.SINGLETON);
        configBinder(binder).bindConfig(BigQueryConfig.class);
    }

    @Provides
    @Singleton
    public BigQueryCredentialsSupplier provideBigQueryCredentialsSupplier(BigQueryConfig config)
    {
        return new BigQueryCredentialsSupplier(config.getCredentialsKey(), config.getCredentialsFile());
    }

    @Provides
    @Singleton
    public BigQueryClient provideBigQueryClient(BigQueryConfig config, HeaderProvider headerProvider, BigQueryCredentialsSupplier bigQueryCredentialsSupplier)
    {
        String billingProjectId = calculateBillingProjectId(config.getParentProjectId(), bigQueryCredentialsSupplier.getCredentials());
        BigQueryOptions.Builder options = BigQueryOptions.newBuilder()
                .setHeaderProvider(headerProvider)
                .setProjectId(billingProjectId);
        // set credentials of provided
        bigQueryCredentialsSupplier.getCredentials().ifPresent(options::setCredentials);
        return new BigQueryClient(options.build().getService(), config);
    }

    // Note that at this point the config has been validated, which means that option 2 or option 3 will always be valid
    static String calculateBillingProjectId(Optional<String> configParentProjectId, Optional<Credentials> credentials)
    {
        // 1. Get from configuration
        if (configParentProjectId.isPresent()) {
            return configParentProjectId.get();
        }
        // 2. Get from the provided credentials, but only ServiceAccountCredentials contains the project id.
        // All other credentials types (User, AppEngine, GCE, CloudShell, etc.) take it from the environment
        if (credentials.isPresent() && credentials.get() instanceof ServiceAccountCredentials) {
            return ((ServiceAccountCredentials) credentials.get()).getProjectId();
        }
        // 3. No configuration was provided, so get the default from the environment
        return BigQueryOptions.getDefaultProjectId();
    }
}

@@ -3220,61 +3220,8 @@
 ion%0A
-        if (configParentProjectId.isPresent()) %7B%0A    
     
@@ -3256,15 +3256,8 @@
 ctId
-.get();
 %0A   
@@ -3253,34 +3253,32 @@
 ojectId%0A        
-%7D%0A
         // 2. Ge
@@ -3378,16 +3378,24 @@
         
+    
+    
 // All o
@@ -3499,117 +3499,218 @@
     
+     
-if (credentials.isPresent() && credentials.get() instanceof ServiceAccountCredentials) %7B%0A            return (
+   .orElseGet(() -%3E credentials%0A                        .filter(ServiceAccountCredentials.class::isInstance)%0A                        .map(ServiceAccountCredentials.class::cast)%0A                        .map
 (Ser
@@ -3735,29 +3735,10 @@
 ials
-) credentials.get()).
+::
 getP
@@ -3749,20 +3749,20 @@
 ctId
-();%0A
+)%0A  
         
 %7D%0A  
@@ -3761,18 +3761,18 @@
     
-%7D%0A
+  
         
 // 3
@@ -3767,16 +3767,20 @@
         
+    
 // 3. No
@@ -3856,23 +3856,43 @@
         
-return 
+                .orElseGet(
 BigQuery
@@ -3898,17 +3898,18 @@
 yOptions
-.
+::
 getDefau
@@ -3919,17 +3919,17 @@
 rojectId
-(
+)
 );%0A    %7D

Clean up Optional usage in BigQueryConnectorModule