package com.rosetta.messenger.network.connection

import android.content.Context

class RuntimeInitializationCoordinator(
    private val ensureConnectionSupervisor: () -> Unit,
    private val setupPacketHandlers: () -> Unit,
    private val setupStateMonitoring: () -> Unit,
    private val setAppContext: (Context) -> Unit,
    private val hasBoundDependencies: () -> Boolean,
    private val addLog: (String) -> Unit
) {
    @Volatile private var packetHandlersRegistered = false
    @Volatile private var stateMonitoringStarted = false

    fun initialize(context: Context) {
        setAppContext(context.applicationContext)
        if (!hasBoundDependencies()) {
            addLog("⚠️ initialize called before dependencies were bound via DI")
        }
        ensureConnectionSupervisor()

        if (!packetHandlersRegistered) {
            setupPacketHandlers()
            packetHandlersRegistered = true
        }
        if (!stateMonitoringStarted) {
            setupStateMonitoring()
            stateMonitoringStarted = true
        }
    }
}
