import uuid
import hashlib
import random


def create_random_index(index_client, did=None, version=None, hashes=None):
    """
    Shorthand for creating new index entries for test purposes.
    Note:
        Expects index client v1.5.2 and above
    Args:
        index_client (indexclient.client.IndexClient): pytest fixture for index_client
        passed from actual test functions
        did (str): if specified it will be used as document did, else allows indexd to create one
        version (str): version of the index being added
        hashes (dict): hashes to store on the index, if not specified a random one is created
    Returns:
        indexclient.client.Document: the document just created
    """

    did = str(uuid.uuid4()) if did is None else did

    if not hashes:
        md5_hasher = hashlib.md5()
        md5_hasher.update(did.encode("utf-8"))
        hashes = {"md5": md5_hasher.hexdigest()}

    doc = index_client.create(
        did=did,
        hashes=hashes,
        size=random.randint(10, 1000),
        version=version,
        acl=["a", "b"],
        authz=["/gen3/programs/a/projects/b"],
        file_name="{}_warning_huge_file.svs".format(did),
        urls=["s3://super-safe.com/{}_warning_huge_file.svs".format(did)],
        urls_metadata={
            "s3://super-safe.com/{}_warning_huge_file.svs".format(did): {"a": "b"}
        },
    )

    return doc