from typing import Optional
import requests


def get_collection_uuid(gc, collection_name: str) -> Optional[str]:
    """Returns the DSA collection uuid from the provided `collection_name`

    Args:
        gc: girder client
        collection_name (string): name of the collection in DSA

    Returns:
        string: DSA collection uuid. None if nothing matches the collection name or an
            error in the get request
    """

    try:
        get_collection_id_response = gc.get(
            f"/collection?text={collection_name}"
        )
    except requests.exceptions.HTTPError as err:
        print(
            f"Error in collection id get request: {err.response.status_code}, {err.response.text}"
        )
        return None

    collection_id_dicts = get_collection_id_response

    for collection_id_dict in collection_id_dicts:
        print("collection_id_dict", collection_id_dict)
        if collection_id_dict["name"] == collection_name:
            collection_id = collection_id_dict["_id"]
            print(
                f"Collection {collection_name} found with id: {collection_id}"
            )
            return collection_id

    print(f"Collection {collection_name} not found")
    return None