from typing import Optional
from typing import List
import requests
from pathlib import Path
import re


def query_folder(
    folder: Optional[str] = None,
    pattern: Optional[str] = None,
    github_url: str = "https://github.com/Ouranosinc/xclim-testdata",
    branch: str = "main",
) -> List[str]:
    """
    Lists the files available for retrieval from a remote git repository with get_file.
    If provided a folder name, will perform a globbing-like filtering operation for parent folders.

    Parameters
    ----------
    folder : str, optional
        Relative pathname of the sub-folder from the top-level.
    pattern : str, optional
        Regex pattern to identify a file.
    github_url : str
        URL to Github repository where the data is stored.
    branch : str, optional
        For GitHub-hosted files, the branch to download from.

    Returns
    -------
    List[str]
    """
    repo_name = github_url.strip("https://github.com/")

    url = f"https://api.github.com/repos/{repo_name}/git/trees/{branch}?recursive=1"
    r = requests.get(url)
    res = r.json()

    md5_files = [f["path"] for f in res["tree"] if f["path"].endswith(".md5")]
    if folder:
        folder = "/".join("/".split(folder)) if "/" in folder else folder
        md5_files = [f for f in md5_files if folder in Path(f).parent.as_posix()]
    files = [re.sub(".md5$", "", f) for f in md5_files]

    if pattern:
        regex = re.compile(pattern)
        files = [string for string in files if re.search(regex, string)]

    return files