"""Read-only queries combining installed-map presence with UMC categorization."""

from pathlib import Path

from core import constants
from core.domain.umc_operations import find_map_categories

UMCData = dict[str, dict[str, object]]


# ==========================================================================
# GESTION DU DOSSIER MAPS
# ==========================================================================


def get_bsp_files() -> list[Path]:
    """Returns a sorted list of all .bsp files in the maps folder."""
    if not constants.MAPS_FOLDER.exists():
        return []

    return sorted(constants.MAPS_FOLDER.glob("*.bsp"))


def get_map_names() -> list[str]:
    """Returns the names of all maps without the .bsp extension."""
    return [map_file.stem for map_file in get_bsp_files()]


def map_file_exists(map_name: str) -> bool:
    """Checks whether a .bsp file exists."""
    return (constants.MAPS_FOLDER / f"{map_name}.bsp").is_file()


def get_map_file_count() -> int:
    """Returns the total number of .bsp files."""
    return len(get_bsp_files())


# ==========================================================================
# LIENS ENTRE LES MAPS ET LES CATÉGORIES
# ==========================================================================


def get_map_categories(umc_data: UMCData, map_name: str) -> list[str]:
    """Returns all categories containing a map."""
    return find_map_categories(umc_data, map_name)


def map_is_categorized(umc_data: UMCData, map_name: str) -> bool:
    """Returns True if the map belongs to at least one category."""
    return bool(find_map_categories(umc_data, map_name))


def get_uncategorized_maps(umc_data: UMCData) -> list[str]:
    """Returns every installed map that is not present in any category."""
    uncategorized = []

    for map_name in get_map_names():
        if not map_is_categorized(umc_data, map_name):
            uncategorized.append(map_name)

    return uncategorized


def get_categorized_maps(umc_data: UMCData) -> list[str]:
    """Returns every installed map that belongs to at least one category."""
    categorized = []

    for map_name in get_map_names():
        if map_is_categorized(umc_data, map_name):
            categorized.append(map_name)

    return categorized
