from pathlib import Path
from typing import List
from typing import Dict


def define_expected_envrc_variables(
    folder: Path,
    exclude_folders: List[str],
    exclude_root_folders: List[str],
    exclude_sub_folders_in_parent_folders: List[str],
) -> Dict[str, Path]:
    """Get the expected export directory variables and values in the `.envrc` file of
    the `govcookiecutter` template.

    Args:
        folder: A folder path to the `govcookiecutter` template folder.
        exclude_folders: A list of folder names to ignore when encountered.
        exclude_root_folders: A list of root-level folder names in `folder` to ignore.
        exclude_sub_folders_in_parent_folders: A list of folder names at the root-level
            of `folder` where their sub-folders should be ignored.

    Returns:
        A dictionary where the keys are expected export directory variables, and the
        values are the expected export directory values of the `.envrc` file in the
        `govcookiecutter` template directory `folder`.

    """

    # Get the names of all root-level directories in `folder`, where each name is in
    # upper case in the format "DIR_<<<DIRECTORY_NAME>>>". Ignore any directories
    # with a name in `exclude_root_folders`
    envrc_expected_dir_variable = {
        f"DIR_{d.name.upper()}": d
        for d in folder.glob("*")
        if d.is_dir() and d.name not in exclude_root_folders
    }

    # Get all sub-directory names one-level down from `folder`, where the directory
    # name is not in `exclude_folders`, and the parent directory name is not in
    # `exclude_sub_folders_in_parent_folders`. Each name is in upper case in the
    # format "DIR_<<<PARENT_DIRECTORY_NAME>>>_<<<DIRECTORY_NAME>>>". Return
    # `envrc_expected_dir_variable`
    for d in folder.glob("*/*"):
        if (
            d.is_dir()
            and d.name not in exclude_folders
            and d.parent.name not in exclude_sub_folders_in_parent_folders
        ):
            envrc_expected_dir_variable[
                f"DIR_{d.parent.name.upper()}_{d.name.upper()}"
            ] = d
    return envrc_expected_dir_variable