package com.correx.infrastructure.tools

import com.correx.core.events.events.ToolRequest
import com.correx.core.tools.contract.ToolExecutor
import com.correx.core.tools.contract.ToolResult
import com.correx.core.tools.registry.ToolRegistry

class DispatchingToolExecutor(
    private val registry: ToolRegistry
) : ToolExecutor {
    override suspend fun execute(request: ToolRequest): ToolResult {
        val tool = registry.resolve(request.toolName)
            ?: return ToolResult.Failure(
                invocationId = request.invocationId,
                reason = "tool not found: ${request.toolName}",
                recoverable = false
            )

        return (tool as? ToolExecutor)?.execute(request)
            ?: ToolResult.Failure(
                invocationId = request.invocationId,
                reason = "tool ${request.toolName} does not support execution",
                recoverable = false
            )
    }
}
