package com.rosetta.messenger.utils

import android.util.Log
import com.rosetta.messenger.network.ProtocolRuntimeAccess

/**
 * Unified diagnostics channel for ad-hoc troubleshooting.
 *
 * Logcat filter:
 * - tag: rosettadev1
 * - text: rosettadev1
 */
object RosettaDev1Log {
    const val TAG: String = "rosettadev1"
    private const val PREFIX = "rosettadev1 | "

    private fun mirrorToProtocol(message: String, error: Throwable? = null) {
        if (!ProtocolRuntimeAccess.isInstalled()) return
        runCatching {
            val protocolMessage =
                if (error == null) {
                    "🧪 $PREFIX$message"
                } else {
                    "🧪 $PREFIX$message | ${error.javaClass.simpleName}: ${error.message ?: "unknown"}"
                }
            ProtocolRuntimeAccess.get().addLog(protocolMessage)
        }
    }

    fun d(message: String) {
        Log.d(TAG, "$PREFIX$message")
        mirrorToProtocol(message)
    }

    fun i(message: String) {
        Log.i(TAG, "$PREFIX$message")
        mirrorToProtocol(message)
    }

    fun w(message: String, error: Throwable? = null) {
        val text = "$PREFIX$message"
        if (error == null) {
            Log.w(TAG, text)
        } else {
            Log.w(TAG, text, error)
        }
        mirrorToProtocol(message, error)
    }

    fun e(message: String, error: Throwable? = null) {
        val text = "$PREFIX$message"
        if (error == null) {
            Log.e(TAG, text)
        } else {
            Log.e(TAG, text, error)
        }
        mirrorToProtocol(message, error)
    }
}
