package com.optix.android.tools.web

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 GobusterTool : BaseTool() {
    override val metadata = ToolMetadata(
        name = "gobuster_termux",
        displayName = "Gobuster (Termux)",
        description = "Fast directory/file/DNS/vhost brute-forcer. Faster than dirb. Requires Termux.",
        category = ToolCategory.DNS_WEB,
        requiresRoot = false,
        requiredPackages = listOf("gobuster"),
        parameters = listOf(
            ToolParam("url", ParamType.STRING, "Target URL (e.g. http://192.168.1.1)"),
            ToolParam("mode", ParamType.ENUM, "Mode", required = false, defaultValue = "dir", enumValues = listOf("dir", "dns", "vhost")),
            ToolParam("wordlist", ParamType.STRING, "Wordlist path", required = false, defaultValue = "/data/data/com.termux/files/usr/share/wordlists/dirb/common.txt"),
            ToolParam("threads", ParamType.INT, "Threads", required = false, defaultValue = "10"),
        ),
        chainSuggestions = listOf("http_inspector", "nikto_termux", "sqli_tester"),
    )
    override suspend fun execute(params: Map<String, Any>, emit: suspend (ToolEvent) -> Unit) {
        val url = paramRequired(params, "url")
        val mode = param(params, "mode") ?: "dir"
        val wordlist = param(params, "wordlist") ?: "/data/data/com.termux/files/usr/share/wordlists/dirb/common.txt"
        val threads = paramInt(params, "threads", 10)
        emit(ToolEvent.Log("[Gobuster] $mode scan on $url with $threads threads..."))
        withContext(Dispatchers.IO) {
            shellExecutor?.executeTermuxAsRoot("gobuster", listOf(mode, "-u", url, "-w", wordlist, "-t", threads.toString()), "gobuster", emit)
        }
        emit(ToolEvent.ChainSuggestion("nikto_termux", mapOf("target" to url)))
        emit(ToolEvent.Done)
    }
}
