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 WhatWebTool : BaseTool() {
    override val metadata = ToolMetadata(
        name = "whatweb_termux",
        displayName = "WhatWeb (Termux)",
        description = "Web technology fingerprinter — identifies CMS, frameworks, server software, analytics. Requires Termux.",
        category = ToolCategory.DNS_WEB,
        requiresRoot = false,
        parameters = listOf(
            ToolParam("url", ParamType.STRING, "Target URL"),
            ToolParam("aggression", ParamType.ENUM, "Aggression level", required = false, defaultValue = "1", enumValues = listOf("1", "3", "4")),
        ),
        chainSuggestions = listOf("http_inspector", "nikto_termux", "gobuster_termux"),
    )
    override suspend fun execute(params: Map<String, Any>, emit: suspend (ToolEvent) -> Unit) {
        val url = paramRequired(params, "url")
        val aggression = param(params, "aggression") ?: "1"
        emit(ToolEvent.Log("[WhatWeb] Fingerprinting $url (aggression $aggression)..."))
        withContext(Dispatchers.IO) {
            shellExecutor?.executeTermuxAsRoot("whatweb", listOf("-a", aggression, url), "ruby", emit)
        }
        emit(ToolEvent.Done)
    }
}
