"""
VYNDR Supabase Client
Singleton Supabase client for Python service.
"""

import os
import logging

logger = logging.getLogger('vyndr')

_client = None


def get_supabase_client():
    """
    Get or create Supabase client singleton.

    Returns:
        Supabase client instance, or None if credentials not configured.
    """
    global _client
    if _client is not None:
        return _client

    url = os.environ.get('SUPABASE_URL')
    key = os.environ.get('SUPABASE_SERVICE_ROLE_KEY')

    if not url or not key:
        logger.warning('[VYNDR] Supabase credentials not configured')
        return None

    try:
        from supabase import create_client
        _client = create_client(url, key)
        return _client
    except ImportError:
        logger.warning('[VYNDR] supabase-py not installed')
        return None
    except Exception as e:
        logger.error(f'[VYNDR] Supabase client init failed: {e}')
        return None
