from pathlib import Path


def verify_directory_structure(path: Path) -> int:
    """Verify that the directory is structured properly

    :return int: Number of future encodings in directory, for progress
        indication
    """
    num_encodings = 0

    if not path.is_dir():
        raise ValueError(f"Base path {path} is not a directory.")

    if next(path.iterdir(), None) is None:
        raise ValueError(f"Base path {path} is empty and has no children.")

    for identity_dir in path.iterdir():

        if not identity_dir.is_dir():
            message = f"{identity_dir} is not a directory. It should be a " \
                      f"directory containing encoding class directories."
            raise ValueError(message)

        for encoding_dir in identity_dir.iterdir():

            if not encoding_dir.is_dir():
                message = f"{encoding_dir} is not a directory. It should be " \
                          f"a directory containing encodings."
                raise ValueError(message)

            for file_path in encoding_dir.iterdir():
                if not file_path.is_file():
                    message = f"{file_path} is not a file. It should be a " \
                              f"file containing encoding data."
                    raise ValueError(message)

                num_encodings += 1

    return num_encodings