import requests
from bs4 import BeautifulSoup
import re


def scrape_vpc_service_controls_supported_products() -> dict:
    """
    scrapes the web page https://cloud.google.com/vpc-service-controls/docs/supported-products. The page
    describes which Google Cloud Platform services are supported and at which support level: ga, beta or preview.

    :return: dictionary with a list of service names per support level
    """
    page = requests.get(
        "https://cloud.google.com/vpc-service-controls/docs/supported-products"
    )

    soup = BeautifulSoup(page.content, "html.parser")
    tables = soup.find_all("table", class_="supported-product-details")

    result = {}
    for table in tables:
        values = [r.get_text().strip() for r in table.find_all("td")]
        details = {values[i]: values[i + 1] for i in range(0, len(values), 2)}
        status = details.get("Status")
        service_name = details.get("Service name")
        if service_name and status:
            status = status.split(".", 1)[0]
            service_names = [
                re.sub(r"\s+(beta)?\s*", "", s) for s in re.split(r",", service_name)
            ]
            if status not in result:
                result[status] = set()
            result[status].update(service_names)
    return result