package ru.forbion.f7cloud.feature.talknative

import android.content.Context
import android.util.Log
import ru.forbion.f7cloud.core.auth.AuthSession
import ru.forbion.f7cloud.core.auth.AuthStore

/**
 * Hooks F7cloud auth into talk-android runtime (User DB, capabilities) for native WebRTC.
 */
object TalkVendorBootstrap {
    private const val TAG = "TalkVendorBootstrap"

    fun onApplicationCreate(context: Context) {
        if (!TalkNativeConfig.bootstrapVendorRuntime) return
        val session = AuthStore(context).load() ?: return
        runCatching { TalkVendorUserSync.ensureUser(context, session) }
            .onFailure { Log.w(TAG, "Talk user sync failed: ${it.message}") }
    }

    fun ensureSessionReady(context: Context, session: AuthSession): Boolean {
        if (!TalkNativeConfig.bootstrapVendorRuntime) return true
        return runCatching { TalkVendorUserSync.ensureUser(context, session) }
            .onFailure { Log.w(TAG, "Talk user sync failed: ${it.message}") }
            .getOrDefault(false)
    }
}
