#!/usr/bin/env python3
# /// script
# requires-python = ">=3.12"
# dependencies = ["httpx"]
# ///
"""Fetch CloudFormation resource types from Resource Specification."""

import json

import httpx

SPEC_URL = "https://d1uauaxba7bl26.cloudfront.net/latest/gzip/CloudFormationResourceSpecification.json"


def main():
    print("Downloading CloudFormation Resource Specification...")
    resp = httpx.get(SPEC_URL)
    resp.raise_for_status()
    spec = resp.json()

    resource_types = sorted(spec.get("ResourceTypes", {}).keys())
    print(f"Found {len(resource_types)} CloudFormation resource types")

    with open("aws_cloudformation.json", "w") as f:
        json.dump(resource_types, f, indent=2)

    print("Saved to aws_cloudformation.json")

    # Extract unique service names from AWS:: resources
    services = set()
    for rt in resource_types:
        if rt.startswith("AWS::"):
            parts = rt.split("::")
            if len(parts) >= 2:
                services.add(parts[1])

    services = sorted(services)
    print(f"Found {len(services)} unique CloudFormation services")

    with open("aws_cloudformation_services.json", "w") as f:
        json.dump(services, f, indent=2)

    print("Saved to aws_cloudformation_services.json")


if __name__ == "__main__":
    main()
