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 WpScanTool : BaseTool() {
    override val metadata = ToolMetadata(
        name = "wpscan_termux",
        displayName = "WPScan (Termux)",
        description = "WordPress vulnerability scanner — finds plugins, themes, users, and known CVEs. Requires Termux.",
        category = ToolCategory.DNS_WEB,
        requiresRoot = false,
        parameters = listOf(
            ToolParam("url", ParamType.STRING, "WordPress site URL"),
            ToolParam("enumerate", ParamType.ENUM, "Enumerate", required = false, defaultValue = "vp", enumValues = listOf("vp", "vt", "u", "ap", "at")),
            ToolParam("api_token", ParamType.STRING, "WPScan API token (optional)", required = false, defaultValue = ""),
        ),
        chainSuggestions = listOf("http_inspector", "gobuster_termux", "nikto_termux"),
    )
    override suspend fun execute(params: Map<String, Any>, emit: suspend (ToolEvent) -> Unit) {
        val url = paramRequired(params, "url")
        val enumerate = param(params, "enumerate") ?: "vp"
        val token = param(params, "api_token") ?: ""
        emit(ToolEvent.Log("[WPScan] Scanning WordPress at $url (enumerate: $enumerate)..."))
        withContext(Dispatchers.IO) {
            val args = mutableListOf("--url", url, "--enumerate", enumerate, "--no-banner")
            if (token.isNotEmpty()) args.addAll(listOf("--api-token", token))
            shellExecutor?.executeTermuxAsRoot("wpscan", args, "ruby", emit)
        }
        emit(ToolEvent.Done)
    }
}
