from __future__ import annotations

import logging
import os
import threading
from typing import Any

logger = logging.getLogger(__name__)


def is_docker_runtime(config: dict[str, Any]) -> bool:
    mode = os.environ.get("NIANNIAN_MODE") or (config.get("runtime") or {}).get("mode", "auto")
    return mode == "docker"


def schedule_push_after_ingest(store, config: dict[str, Any]) -> None:
    if store is None or not is_docker_runtime(config):
        return
    if not (config.get("sync") or {}).get("push_after_ingest"):
        return

    def _run() -> None:
        try:
            store.flush_to_cloud(force=True)
        except Exception:
            logger.exception("push_after_ingest failed")

    threading.Thread(target=_run, daemon=True).start()
