package com.optix.android.tools.network

import com.optix.android.domain.model.ParamType
import com.optix.android.domain.model.ToolCategory
import com.optix.android.domain.model.ToolEvent
import com.optix.android.domain.model.ToolMetadata
import com.optix.android.domain.model.ToolParam
import com.optix.android.tools.registry.BaseTool
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class SnmpWalkTool : BaseTool() {
    override val metadata = ToolMetadata(
        name = "snmpwalk_termux",
        displayName = "SNMPwalk (Termux)",
        description = "SNMP enumeration — walks the MIB tree to extract device info, interfaces, running processes. Requires Termux.",
        category = ToolCategory.NETWORK_SCANNING,
        requiresRoot = false,
        parameters = listOf(
            ToolParam("target", ParamType.STRING, "Target IP"),
            ToolParam("community", ParamType.STRING, "SNMP community string", required = false, defaultValue = "public"),
            ToolParam("version", ParamType.ENUM, "SNMP version", required = false, defaultValue = "2c", enumValues = listOf("1", "2c", "3")),
        ),
        chainSuggestions = listOf("nmap_termux", "port_scanner"),
    )
    override suspend fun execute(params: Map<String, Any>, emit: suspend (ToolEvent) -> Unit) {
        val target = paramRequired(params, "target")
        val community = param(params, "community") ?: "public"
        val version = param(params, "version") ?: "2c"
        emit(ToolEvent.Log("[SNMPwalk] Walking $target with community '$community' (v$version)..."))
        withContext(Dispatchers.IO) {
            shellExecutor?.executeTermuxAsRoot("snmpwalk", listOf("-v", version, "-c", community, target), "snmp-mibs-downloader", emit)
        }
        emit(ToolEvent.Done)
    }
}
