"""
ICAC Commune Data — données statiques commune
Charge depuis config/commune.json
"""

import json
import logging
from pathlib import Path

log = logging.getLogger("icac.commune")

CONFIG_DIR = Path(__file__).parent.parent / "config"


def get_commune_data(insee: str) -> dict:
    """Load commune data from config/commune.json."""
    config_file = CONFIG_DIR / "commune.json"

    if config_file.exists():
        try:
            data = json.loads(config_file.read_text())
            if data.get("insee") == insee:
                return data
        except Exception as e:
            log.warning("Failed to read commune.json: %s", e)

    # Fallback: hardcoded Bessèges data
    if insee == "30034":
        return {
            "nom": "BESSÈGES",
            "nom_court": "Bessèges",
            "insee": "30034",
            "cp": "30160",
            "departement": "30",
            "departement_nom": "GARD",
            "region": "76",
            "region_nom": "OCCITANIE",
            "population": 2847,
            "superficie_km2": 27.4,
            "employes_municipaux": 42,
            "budget_fonctionnement": 3200000,
            "budget_investissement": 1000000,
            "budget_total": "4.2 M€",
            "maire": "M. SALLES",
            "mandat_fin": "2026",
            "nb_conseillers": 15,
            "strate": "< 5 000 HAB.",
            "dernier_conseil": {
                "date": "2026-02-24",
                "date_display": "24 FÉV. 2026",
                "url_pv": "https://www.besseges.fr/conseil-municipal/",
                "nb_deliberations": 8
            },
            "dernier_bulletin": {
                "date": "2026-01",
                "date_display": "JAN. 2026",
                "url": "https://www.besseges.fr/bulletin-municipal/"
            },
            "sources_actives": [
                "data.gouv.fr", "sirene", "aides-territoires",
                "jo-associations", "boamp"
            ],
            "docs_indexes": 47
        }

    return None
