package com.tomhempel.rbarometer.network

import android.util.Log
import com.tomhempel.rbarometer.BuildConfig
import java.security.MessageDigest

/** Debug-gated logging for sync/API paths (avoids logging full JSON bodies in production). */
internal object SyncLog {
    fun d(tag: String, message: String) {
        if (BuildConfig.DEBUG) Log.d(tag, message)
    }

    fun w(tag: String, message: String) {
        if (BuildConfig.DEBUG) Log.w(tag, message)
    }

    fun e(tag: String, message: String, throwable: Throwable? = null) {
        if (!BuildConfig.DEBUG) return
        if (throwable != null) Log.e(tag, message, throwable) else Log.e(tag, message)
    }

    fun apiResponse(tag: String, label: String, httpCode: Int, body: String?) {
        if (!BuildConfig.DEBUG) return
        val bytes = body?.length ?: 0
        Log.d(tag, "$label → HTTP $httpCode (${bytes} bytes)")
    }

    fun clientRef(clientCode: String?): String {
        val normalized = clientCode?.trim().orEmpty()
        if (normalized.isEmpty()) return "client=<blank>"
        val digest = MessageDigest.getInstance("SHA-256")
            .digest(normalized.uppercase().toByteArray(Charsets.UTF_8))
            .joinToString("") { "%02x".format(it) }
            .take(8)
        return "clientHash=$digest"
    }
}
