package com.correx.infrastructure.tools

import com.correx.core.tools.contract.Tool
import com.correx.infrastructure.tools.filesystem.FileEditTool
import com.correx.infrastructure.tools.filesystem.FileReadTool
import com.correx.infrastructure.tools.filesystem.FileWriteTool
import com.correx.infrastructure.tools.shell.ShellTool
import java.nio.file.Path

data class ToolConfig(
    val shell: ShellConfig = ShellConfig(),
    val fileRead: FileReadConfig = FileReadConfig(),
    val fileWrite: FileWriteConfig = FileWriteConfig(),
    val fileEdit: FileEditConfig = FileEditConfig(),
)

data class FileReadConfig(val enabled: Boolean = false, val allowedPaths: Set<Path> = emptySet())
data class FileWriteConfig(
    val enabled: Boolean = false,
    val allowedPaths: Set<Path> = emptySet(),
    val workingDir: Path? = null,
)

data class FileEditConfig(
    val enabled: Boolean = false,
    val allowedPaths: Set<Path> = emptySet(),
    val workingDir: Path? = null,
)

data class ShellConfig(
    val enabled: Boolean = false,
    val allowedExecutables: Set<String> = emptySet(),
    val workingDir: Path? = null,
)

/**
 * Extension function to convert [ToolConfig] into a list of [Tool] implementations.
 */
fun ToolConfig.buildTools(): List<Tool> = buildList {
    if (shell.enabled) {
        add(
            ShellTool(
                allowedExecutables = shell.allowedExecutables,
                workingDir = shell.workingDir,
            ),
        )
    }
    if (fileRead.enabled) {
        add(
            FileReadTool(
                allowedPaths = fileRead.allowedPaths,
            ),
        )
    }
    if (fileWrite.enabled) {
        add(
            FileWriteTool(
                allowedPaths = fileWrite.allowedPaths,
                workingDir = fileWrite.workingDir,
            ),
        )
    }
    if (fileEdit.enabled) {
        add(
            FileEditTool(
                allowedPaths = fileEdit.allowedPaths,
                workingDir = fileEdit.workingDir,
            ),
        )
    }
}
