from __future__ import annotations

from threading import Lock

from govcrawler.settings import get_settings

_lock = Lock()
_rag_export_after_crawl_enabled: bool | None = None


def is_rag_export_after_crawl_enabled() -> bool:
    with _lock:
        if _rag_export_after_crawl_enabled is not None:
            return _rag_export_after_crawl_enabled
    return bool(get_settings().rag_export_after_crawl_enabled)


def set_rag_export_after_crawl_enabled(enabled: bool) -> bool:
    global _rag_export_after_crawl_enabled
    with _lock:
        _rag_export_after_crawl_enabled = bool(enabled)
        return _rag_export_after_crawl_enabled


def rag_export_after_crawl_config() -> dict[str, bool]:
    env_default = bool(get_settings().rag_export_after_crawl_enabled)
    with _lock:
        override = _rag_export_after_crawl_enabled
    return {
        "enabled": env_default if override is None else override,
        "env_default": env_default,
        "overridden": override is not None,
    }
