/*
 * Copyright 2025 Google LLC
 *
 * 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 genai.tools;

// [START googlegenaisdk_tools_google_search_and_urlcontext_with_txt]

import com.google.genai.Client;
import com.google.genai.types.Candidate;
import com.google.genai.types.Content;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.GoogleSearch;
import com.google.genai.types.HttpOptions;
import com.google.genai.types.Tool;
import com.google.genai.types.UrlContext;
import java.util.List;

public class ToolsGoogleSearchAndUrlContextWithTxt {

  public static void main(String[] args) {
    // TODO(developer): Replace these variables before running the sample.
    String modelId = "gemini-2.5-flash";
    String url = "https://www.google.com/search?q=events+in+New+York";
    generateContent(modelId, url);
  }

  // Generates content with the Url Context and Google Search Tools.
  public static String generateContent(String modelId, String url) {
    // Client Initialization. Once created, it can be reused for multiple requests.
    try (Client client =
        Client.builder()
            .location("global")
            .vertexAI(true)
            .httpOptions(HttpOptions.builder().apiVersion("v1beta1").build())
            .build()) {

      // Set the Url Context and Google Search tools.
      List<Tool> tools =
          List.of(
              Tool.builder().urlContext(UrlContext.builder().build()).build(),
              Tool.builder().googleSearch(GoogleSearch.builder().build()).build());

      String prompt =
          String.format(
              "Give me three day events schedule based on %s. Also let me know what"
                  + " needs to be taken care of considering weather and commute.",
              url);

      GenerateContentResponse response =
          client.models.generateContent(
              modelId,
              prompt,
              GenerateContentConfig.builder().tools(tools).responseModalities("TEXT").build());

      // Get response candidate.
      Candidate candidate =
          response
              .candidates()
              .flatMap(candidates -> candidates.stream().findFirst())
              .orElseThrow(
                  () ->
                      new IllegalStateException(
                          "No response candidate was generated by the model."));

      // Print parts of the response.
      candidate
          .content()
          .flatMap(Content::parts)
          .ifPresent(parts -> parts.forEach(part -> part.text().ifPresent(System.out::println)));
      // Example response:
      // Three-Day Event Schedule in...
      // **Day 1: Friday, October 24, 2025**
      // *   **Evening Event:** Attend the **2025...

      candidate.urlContextMetadata().ifPresent(System.out::println);

      return response.text();
    }
  }
}
// [END googlegenaisdk_tools_google_search_and_urlcontext_with_txt]
