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

import re

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

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

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


def parse_resources(soup) -> list[dict]:
    # Extract service prefix: "service prefix: <code class="code">s3</code>"
    prefix_match = soup.find(string=re.compile(r"service prefix:"))
    if not prefix_match:
        return []

    prefix_code = prefix_match.find_next("code", class_="code")
    if not prefix_code:
        return []
    service_prefix = prefix_code.get_text(strip=True)

    # Find resource types table (after h2 with id ending in -resources-for-iam-policies)
    resources_heading = soup.find("h2", id=re.compile(r"-resources-for-iam-policies$"))
    if not resources_heading:
        return []

    table = resources_heading.find_next("table")
    if not table:
        return []

    resources = []
    for row in table.find_all("tr"):
        cells = row.find_all("td")
        if len(cells) < 2:
            continue

        # First cell: resource type name
        resource_cell = cells[0]
        link = resource_cell.find("a")
        resource_type = link.get_text(strip=True) if link else resource_cell.get_text(strip=True)
        if not resource_type:
            continue

        # Second cell: ARN pattern (in <code class="code">)
        arn_cell = cells[1]
        code = arn_cell.find("code", class_="code")
        if not code:
            continue
        arn_pattern = code.get_text(strip=True)

        resources.append({
            "service_prefix": service_prefix,
            "resource_type": resource_type,
            "arn_pattern": arn_pattern,
        })

    return resources

def main():
    services_df = pd.read_parquet("aws_services.parquet")

    resources = []
    for _, row in services_df.iterrows():
        html = fetch_page(row["href"])
        soup = BeautifulSoup(html, "html.parser")
        page_resources = parse_resources(soup)
        resources.extend(page_resources)
        print(f"Parsed {len(page_resources)} resources for service {row['name']}")

    resources_df = pd.DataFrame(resources)
    resources_df.to_parquet("aws_resources.parquet", index=False)
    resources_df.to_excel("aws_resources.xlsx", index=False)
    print(f"Wrote {len(resources_df)} resources to aws_resources.parquet and aws_resources.xlsx")

if __name__ == "__main__":
    main()
