import csv


def get_version_groups(csv_dir, pkm_versions):
    """Get the groups that classify games versions.

    Args:
        csv_dir (pathlib.Path): Path to csv directory.
        pkm_versions (list): List of dict containing infos to build
            pokediadb.models.Versionobject.

    Returns
        list: Returns the list of versions with generation information.

    Raises:
        FileNotFoundError: Raised if version_groups.csv does not exist.

    """
    with (csv_dir / "version_groups.csv").open(encoding="utf8") as f_v_group:
        reader = csv.reader(f_v_group)
        next(reader)  # Skip header

        for row in reader:
            for version in pkm_versions:
                if version.get("group", None) == int(row[0]):
                    version["generation"] = int(row[2])
                    del version["group"]

    return pkm_versions