import logging
import os

logger = logging.getLogger(__name__)

_client = None


def get_client():
    """Return a Supabase client or None if env vars are not set (dev mode)."""
    global _client
    if _client is not None:
        return _client

    url = os.getenv("SUPABASE_URL", "")
    key = os.getenv("SUPABASE_SERVICE_KEY", "")

    if not url or not key:
        return None

    try:
        from supabase import create_client
        _client = create_client(url, key)
        logger.info("Supabase client initialized")
    except Exception as e:
        logger.error(f"Supabase client init failed: {e}")

    return _client
