package com.optix.android.domain.repository

import com.optix.android.domain.model.ToolChain
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Shared bridge between the AI assistant screen and the NETWORK (LanMap) tab.
 *
 * The AI assistant produces a [ToolChain] (e.g. "scan → port scan → vuln check").
 * When the user taps "Send chain to NETWORK", the chain is offered here and the
 * [com.optix.android.ui.screens.lanmap.LanMapViewModel] observes it and enqueues
 * the steps for sequential execution.
 */
@Singleton
class ChainRepository @Inject constructor() {

    private val _pendingChain = MutableSharedFlow<ToolChain>(extraBufferCapacity = 1)
    val pendingChain = _pendingChain.asSharedFlow()

    private val _lastSentChain = MutableStateFlow<ToolChain?>(null)
    val lastSentChain: StateFlow<ToolChain?> = _lastSentChain.asStateFlow()

    /** Offer a chain for the NETWORK tab to pick up. Returns true if there was capacity. */
    fun sendChain(chain: ToolChain): Boolean {
        _lastSentChain.value = chain
        return _pendingChain.tryEmit(chain)
    }
}
