"""Load a GeoJSON boundary as a GeoDataFrame with normalized CRS."""

from __future__ import annotations

from pathlib import Path


def load_boundary_gdf(
    path: "str | Path",
    crs: str = "EPSG:4326",
) -> "GeoDataFrame":
    """Load a GeoJSON file as a GeoDataFrame with a normalized CRS.

    Args:
        path: Path to the GeoJSON file.
        crs: Target CRS string (e.g. "EPSG:4326"). If the file has no CRS
            set, it is assigned this CRS. If the file already has a CRS,
            it is reprojected to this CRS.

    Returns:
        GeoDataFrame with the requested CRS set.

    Raises:
        FileNotFoundError: If the file does not exist.
        fiona.errors.DriverError: If the file is not a valid GeoJSON.
    """
    import geopandas as gpd  # type: ignore

    p = Path(path)
    if not p.exists():
        raise FileNotFoundError(f"GeoJSON file not found: {p}")

    gdf = gpd.read_file(str(p))

    if gdf.crs is None:
        gdf = gdf.set_crs(crs)
    else:
        gdf = gdf.to_crs(crs)

    return gdf
