
from __future__ import annotations

import httpx
from typing import Any

BASE_URL = "https://georisques.gouv.fr/api/v1"
TIMEOUT = httpx.Timeout(30.0, connect=10.0)


async def _get_json(endpoint: str, params: dict[str, str]) -> Any:
    async with httpx.AsyncClient(timeout=TIMEOUT) as client:
        response = await client.get(f"{BASE_URL}/{endpoint.lstrip('/')}", params=params)
        response.raise_for_status()
        return response.json()


async def fetch_risks_by_commune(code_insee: str) -> dict[str, Any]:
    # The API accepts code_insee on several endpoints. The generic "risques"
    # endpoint is enough for a first elected-officials synthesis.
    data = await _get_json("risques", {"code_insee": code_insee})
    return {"code_insee": code_insee, "endpoint": "risques", "data": data}


async def fetch_ppr_by_commune(code_insee: str) -> dict[str, Any]:
    data = await _get_json("ppr", {"code_insee": code_insee})
    return {"code_insee": code_insee, "endpoint": "ppr", "data": data}
