package ru.forbion.f7cloud.feature.talknative

import ru.forbion.f7cloud.core.auth.AuthSession

/** Maps F7cloud [AuthSession] to talk-android account credentials. */
data class TalkAccount(
    val serverUrl: String,
    val userId: String,
    val displayName: String,
    val password: String,
    val trustAllCerts: Boolean,
)

object TalkAuthBridge {
    fun fromSession(session: AuthSession): TalkAccount = TalkAccount(
        serverUrl = session.serverUrl.trimEnd('/'),
        userId = session.davUserId ?: session.username,
        displayName = session.username,
        password = session.appPassword,
        trustAllCerts = session.trustAllCerts,
    )

    /** Basic-auth header value for talk-android OkHttp interceptors. */
    fun basicAuthHeader(session: AuthSession): String {
        val creds = "${session.username}:${session.appPassword}"
        return "Basic ${android.util.Base64.encodeToString(creds.toByteArray(), android.util.Base64.NO_WRAP)}"
    }
}
