"""
Supabase 客户端初始化
"""
from supabase import create_client, Client
from app.core.config import settings
from loguru import logger
from typing import Optional

_supabase_client: Optional[Client] = None


def get_supabase() -> Client:
    """获取 Supabase 客户端单例"""
    global _supabase_client
    
    if _supabase_client is None:
        if not settings.SUPABASE_URL or not settings.SUPABASE_KEY:
            raise ValueError("SUPABASE_URL 和 SUPABASE_KEY 必须在 .env 中配置")
        
        _supabase_client = create_client(
            settings.SUPABASE_URL,
            settings.SUPABASE_KEY
        )
        logger.info("Supabase 客户端已初始化")
    
    return _supabase_client
