"""Shared gateway persistence backend resolution (filesystem | postgres | dual)."""

from __future__ import annotations


def resolve_persist_backend() -> str:
    """Return filesystem | postgres | dual.

    Uses GATEWAY_PERSIST_BACKEND, then INTERVIEW_SESSION_BACKEND, then auto
    (dual on Postgres, filesystem otherwise).
    """
    try:
        from app.config import get_settings
        from app.database import DATABASE_URL

        settings = get_settings()
        raw = (
            getattr(settings, "gateway_persist_backend", None)
            or getattr(settings, "interview_session_backend", None)
            or "auto"
        )
        raw = str(raw).strip().lower()
        if raw == "auto":
            return "dual" if DATABASE_URL.startswith("postgresql") else "filesystem"
        if raw in {"filesystem", "postgres", "dual"}:
            return raw
    except Exception:  # noqa: BLE001
        pass
    return "filesystem"
