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 SmbClientTool : BaseTool() {
    override val metadata = ToolMetadata(
        name = "smbclient_termux",
        displayName = "SMBclient (Termux)",
        description = "List SMB shares and enumerate Windows file sharing. Requires Termux.",
        category = ToolCategory.NETWORK_SCANNING,
        requiresRoot = false,
        parameters = listOf(
            ToolParam("target", ParamType.STRING, "Target IP"),
            ToolParam("username", ParamType.STRING, "Username", required = false, defaultValue = ""),
            ToolParam("password", ParamType.STRING, "Password", required = false, defaultValue = ""),
        ),
        chainSuggestions = listOf("netbios_scanner", "nmap_termux", "hydra_termux"),
    )
    override suspend fun execute(params: Map<String, Any>, emit: suspend (ToolEvent) -> Unit) {
        val target = paramRequired(params, "target")
        val user = param(params, "username") ?: ""
        val pass = param(params, "password") ?: ""
        emit(ToolEvent.Log("[SMBclient] Listing shares on $target..."))
        withContext(Dispatchers.IO) {
            val args = mutableListOf("-L", target, "-N")
            if (user.isNotEmpty()) { args.addAll(listOf("-U", "$user%$pass")) }
            shellExecutor?.executeTermuxAsRoot("smbclient", args, "samba", emit)
        }
        emit(ToolEvent.Done)
    }
}
