#!/usr/bin/env python3
# /// script
# requires-python = ">=3.10"
# dependencies = ["requests", "joblib", "beautifulsoup4", "pandas", "pyarrow"]
# ///

from urllib.parse import urljoin

import joblib
import pandas as pd
import requests
from bs4 import BeautifulSoup

URL = "https://docs.aws.amazon.com/service-authorization/latest/reference/reference_policies_actions-resources-contextkeys.html"

memory = joblib.Memory(".cache", verbose=0)

@memory.cache
def fetch_page(url):
    r = requests.get(url)
    r.raise_for_status()
    return r.text


def main():
    content = fetch_page(URL)
    soup = BeautifulSoup(content, "html.parser")

    # Find the highlights div containing the service list
    highlights = soup.find("div", class_="highlights")
    services = []

    for li in highlights.find_all("li"):
        link = li.find("a")
        if not link:
            raise ValueError("No link found in list item")

        name = link.get_text(strip=True)
        if not name:
            raise ValueError("Link has no text")

        href = link.get("href", "")  # e.g., ./list_awsaccountmanagement.html
        if not href:
            raise ValueError("Link has no href")

        # Extract slug: ./list_awsaccountmanagement.html -> awsaccountmanagement
        slug = href.removeprefix("./list_").removesuffix(".html")
        absolute_url = urljoin(URL, href)
        services.append({"name": name, "href": absolute_url, "slug": slug})

    df = pd.DataFrame(services)
    df.to_parquet("aws_services.parquet", index=False)
    print(f"Wrote {len(df)} services to aws_services.parquet")


if __name__ == "__main__":
    main()
