"""Authenticated Supabase client factory.

Provides a singleton async client that shares credentials with the AuthHandler.
"""

import asyncio

from supabase.lib.client_options import AsyncClientOptions

from harbor.auth.constants import SUPABASE_PUBLISHABLE_KEY, SUPABASE_URL
from harbor.auth.file_storage import AsyncFileStorage
from supabase import AsyncClient, acreate_client

_client: AsyncClient | None = None
_init_lock: asyncio.Lock | None = None


def _get_init_lock() -> asyncio.Lock:
    global _init_lock
    if _init_lock is None:
        _init_lock = asyncio.Lock()
    return _init_lock


def reset_client() -> None:
    """Reset the singleton client so a new event loop can create a fresh one."""
    global _client, _init_lock
    _client = None
    _init_lock = None


async def create_authenticated_client() -> AsyncClient:
    """Return the shared Supabase async client, creating it on first call."""
    global _client
    if _client is not None:
        return _client
    async with _get_init_lock():
        _client = await acreate_client(
            SUPABASE_URL,
            SUPABASE_PUBLISHABLE_KEY,
            options=AsyncClientOptions(storage=AsyncFileStorage()),
        )
        return _client
