import os
import urllib3
from selenium import webdriver
import google.generativeai as genai

# Disable warnings and set environment variables to suppress verbose logs
urllib3.disable_warnings()
os.environ["GRPC_VERBOSITY"] = "ERROR"
os.environ["GLOG_minloglevel"] = "2"

def scrape(google_api_key, url, instructions, model="gemini-exp-1206"):
    """
    Scrape the provided URL and generate a response using Google's Generative AI model.
    
    Parameters:
        google_api_key (str): Your API key for Google Generative AI.
        url (str): The target URL to scrape.
        instructions (str): The instructions to pass to the generative model.
        model (str): The generative model name (default: gemini-exp-1206).
    
    Returns:
        str: The text generated by the AI model.
    """
    # Configure the generative AI with the provided API key.
    genai.configure(api_key=google_api_key)
    
    # Define the generation configuration.
    generation_config = {
        "temperature": 1,
        "top_p": 0.95,
        "top_k": 64,
        "max_output_tokens": 8192,
        "response_mime_type": "text/plain",
    }
    
    # Set up Selenium WebDriver with headless Chrome.
    options = webdriver.ChromeOptions()
    options.add_argument("--headless=new")
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    options.add_argument('--log-level=3')
    options.add_argument('log-level=3')
    os.environ['WDM_LOG_LEVEL'] = '0'
    
    # Initialize WebDriver (make sure chromedriver is in your PATH).
    driver = webdriver.Chrome(options=options)
    driver.get(url)
    
    html = driver.page_source
    driver.quit()
    
    # Create the Generative Model.
    model_instance = genai.GenerativeModel(
        model_name=model,
        generation_config=generation_config,
    )
    
    # Start a chat session with the model using the provided instructions
    # and the scraped HTML enclosed within triple backticks.
    chat_session = model_instance.start_chat(
        history=[
            {
                "role": "user",
                "parts": [instructions + "```" + html + "```"],
            }
        ]
    )
    
    # Send the input text to the chat session and return the cleaned response.
    response = chat_session.send_message("INSERT_INPUT_HERE")
    return response.text.replace("```json", "").replace("```", "")
