"""Resolved, seeded paths for the librarian's runtime JSON state.

``conjurer_librarian.py`` and ``scrape_bot.py`` open these files in place with
mode ``r+``, which requires them to already exist - in a fresh container the
working directory has none of them, so the worker threads crashed with
FileNotFoundError.

They now live in a single mounted, persistent directory
(``CONJURER_LIBRARIAN_STATE_DIR``, default ``/lib_temp_files``) and are seeded
with an empty JSON object on import, so a fresh container/volume never crashes
and the accumulated results survive restarts.
"""
import os

STATE_DIR = os.getenv("CONJURER_LIBRARIAN_STATE_DIR", "/lib_temp_files")

CR_RESULTS = os.path.join(STATE_DIR, "cr_results.json")   # Crossref raw hits
RR_RESULTS = os.path.join(STATE_DIR, "rr_results.json")   # refined results
NOT_IN_DB = os.path.join(STATE_DIR, "not_in_db.json")     # DOIs to scrape
S_RESULTS = os.path.join(STATE_DIR, "s_results.json")     # final send results

_ALL = (CR_RESULTS, RR_RESULTS, NOT_IN_DB, S_RESULTS)


def ensure_state_files():
    """Create the state dir and seed any missing file with an empty JSON dict."""
    try:
        os.makedirs(STATE_DIR, exist_ok=True)
    except OSError as exc:  # pragma: no cover - surfaced in logs, not fatal
        print(f"lib_paths: cannot create {STATE_DIR}: {exc}")
        return
    for path in _ALL:
        if not os.path.exists(path):
            try:
                with open(path, "w", encoding="utf-8") as handle:
                    handle.write("{}")
            except OSError as exc:  # pragma: no cover
                print(f"lib_paths: cannot seed {path}: {exc}")


ensure_state_files()
