"""Conference abstract DB adapters.

각 학회별 mock JSON loader. 외부 네트워크 호출은 절대 하지 않으며
모든 데이터는 합성(가상)이다.

향후 enhancement: 실제 학회 abstract 검색 API 연동
(현재는 표준 라이브러리만 사용하는 mock 로더만 제공).
"""

import json
from pathlib import Path


CONFERENCES = [
    {"key": "ada", "name": "ADA 2026", "file": "ada_2026_mock.json",
     "full_name": "American Diabetes Association Scientific Sessions 2026",
     "month": "2026-06"},
    {"key": "easd", "name": "EASD 2026", "file": "easd_2026_mock.json",
     "full_name": "European Association for the Study of Diabetes 2026",
     "month": "2026-09"},
    {"key": "obesity_week", "name": "Obesity Week 2026", "file": "obesity_week_2026_mock.json",
     "full_name": "Obesity Week 2026 (TOS/ASMBS)",
     "month": "2026-11"},
    {"key": "endo", "name": "ENDO 2026", "file": "endo_2026_mock.json",
     "full_name": "ENDO 2026 (Endocrine Society)",
     "month": "2026-06"},
    {"key": "eco", "name": "ECO 2026", "file": "eco_2026_mock.json",
     "full_name": "European Congress on Obesity 2026",
     "month": "2026-05"},
]


def _data_dir():
    return Path(__file__).resolve().parent.parent / "data"


def load_conference(conf_key, offline=True):
    """Load abstracts for one conference.

    Args:
        conf_key: 'ada' | 'easd' | 'obesity_week' | 'endo' | 'eco'
        offline: If True (default and only supported mode), load from mock JSON.
                 Network mode is not implemented and would raise.

    Returns:
        list of abstract dicts.
    """
    if not offline:
        raise NotImplementedError(
            "Network mode disabled. ObesityPharmaWatch only supports --offline."
        )
    conf = next((c for c in CONFERENCES if c["key"] == conf_key), None)
    if conf is None:
        raise ValueError(f"Unknown conference key: {conf_key}")
    path = _data_dir() / conf["file"]
    with open(path, "r", encoding="utf-8") as fh:
        data = json.load(fh)
    # Attach conference metadata to each abstract record.
    for rec in data:
        rec["_conference"] = conf["name"]
        rec["_conference_key"] = conf["key"]
        rec["_conference_month"] = conf["month"]
    return data


def load_all(offline=True):
    """Load abstracts from all 5 conferences."""
    out = []
    for conf in CONFERENCES:
        out.extend(load_conference(conf["key"], offline=offline))
    return out


def load_ctg_trials(offline=True):
    """Load mock ClinicalTrials.gov v2 records."""
    if not offline:
        raise NotImplementedError("Network mode disabled.")
    path = _data_dir() / "ctg_obesity_mock.json"
    with open(path, "r", encoding="utf-8") as fh:
        return json.load(fh)
