"""
02_data_acquisition_wgi.py
World Bank Worldwide Governance Indicators (WGI) 数据获取模块
World Bank WGI Data Acquisition Module

用途 / Purpose:
    通过 World Bank data360 REST API 获取 WGI 2025 修订版六项治理指标
    （话语权与问责、政治稳定性、政府效能、监管质量、法治、腐败控制），
    覆盖 OECD-38 成员国 2000-2023 年（2001 年因 WGI 当年未发布，后续以
    线性插值处理，见 03_preprocess_construct.py）。

    Retrieves the six WGI 2025-revision governance indicators via the
    World Bank data360 REST API for the OECD-38 sample, 2000-2023
    (2001 was not published by WGI and is linearly interpolated later).

依赖 / Dependencies: requests, pandas
"""

import json
import logging
import time
from pathlib import Path

import pandas as pd

try:
    import requests
except ImportError:  # pragma: no cover
    requests = None

logging.basicConfig(level=logging.INFO, format="%(asctime)s  %(message)s")
log = logging.getLogger("02_data_acquisition_wgi")

OUTPUT_DIR = Path("./data")
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)

DATA360_BASE = "https://data360api.worldbank.org/data360/data"

WGI_INDICATORS = {
    "WB_WGI_VA_EST": "voice_accountability",
    "WB_WGI_PV_EST": "political_stability",
    "WB_WGI_GE_EST": "government_effectiveness",
    "WB_WGI_RQ_EST": "regulatory_quality",
    "WB_WGI_RL_EST": "rule_of_law",
    "WB_WGI_CC_EST": "corruption_control",
}

OECD38_ISO3 = [
    "AUS", "AUT", "BEL", "CAN", "CHE", "CHL", "COL", "CRI", "CZE", "DEU",
    "DNK", "ESP", "EST", "FIN", "FRA", "GBR", "GRC", "HUN", "IRL", "ISL",
    "ISR", "ITA", "JPN", "KOR", "LTU", "LUX", "LVA", "MEX", "NLD", "NOR",
    "NZL", "POL", "PRT", "SVK", "SVN", "SWE", "TUR", "USA",
]

YEARS = list(range(2000, 2024))


def fetch_wgi_indicator(dataset_id: str) -> pd.DataFrame:
    """Fetch one WGI indicator series for the full OECD-38 sample."""
    if requests is None:
        raise RuntimeError("requests not installed; cannot perform live fetch.")
    params = {
        "DATABASE_ID": "WB_WGI",
        "INDICATOR": dataset_id,
        "REF_AREA": ",".join(OECD38_ISO3),
        "timePeriod": f"{YEARS[0]}:{YEARS[-1]}",
    }
    resp = requests.get(DATA360_BASE, params=params, timeout=30)
    resp.raise_for_status()
    payload = resp.json()
    rows = payload.get("value", [])
    return pd.DataFrame(rows)


def main():
    frames = []
    coverage = {}
    for code, name in WGI_INDICATORS.items():
        try:
            df = fetch_wgi_indicator(code)
            df["indicator_name"] = name
            frames.append(df)
            coverage[name] = len(df)
            time.sleep(0.3)
        except Exception as exc:  # noqa: BLE001
            log.warning("WGI fetch failed for %s: %s", code, exc)
            coverage[name] = 0

    if frames:
        panel = pd.concat(frames, ignore_index=True)
        panel.to_csv(OUTPUT_DIR / "wgi_raw.csv", index=False)
        log.info("Saved WGI raw data -> data/wgi_raw.csv (%d rows)", len(panel))

    with open(OUTPUT_DIR / "wgi_acquisition_summary.json", "w", encoding="utf-8") as fh:
        json.dump(
            {
                "indicators": list(WGI_INDICATORS.values()),
                "countries": len(OECD38_ISO3),
                "years": [YEARS[0], YEARS[-1]],
                "coverage_by_indicator": coverage,
                "known_gap": "2001 not published by WGI; imputed via linear "
                             "interpolation in 03_preprocess_construct.py",
            },
            fh, ensure_ascii=False, indent=2,
        )
    log.info("WGI acquisition module complete.")


if __name__ == "__main__":
    main()
