#pragma once

#include <nlohmann/json.hpp>
#include <string>
#include <vector>
#include <optional>

namespace celuna::tools {

using json = nlohmann::json;

/**
 * @brief FileSystem tools for agentic LLM (Claude Code style)
 *
 * Tools:
 * - read_file: Read file content with optional offset/limit
 * - write_file: Write/replace entire file
 * - edit_file: Replace exact string in file
 * - list_directory: List directory contents
 * - glob_files: Search files by pattern
 * - grep_files: Search content with regex
 */
class FileSystemTools {
public:
    /**
     * @brief Get tool definitions for LLM
     */
    static std::vector<json> getToolDefinitions();

    /**
     * @brief Execute a tool by name
     * @param toolName Name of the tool
     * @param params Tool parameters
     * @return Result JSON with success/error
     */
    static json execute(const std::string& toolName, const json& params);

    /**
     * @brief Check if tool name is a FileSystem tool
     */
    static bool isFileSystemTool(const std::string& toolName);

    // Configuration
    static void setAllowedPaths(const std::vector<std::string>& paths);
    static void setBlockedPatterns(const std::vector<std::string>& patterns);
    static void setMaxReadSize(size_t bytes);
    static void setMaxWriteSize(size_t bytes);

private:
    // Tool implementations
    static json readFile(const json& params);
    static json writeFile(const json& params);
    static json editFile(const json& params);
    static json listDirectory(const json& params);
    static json globFiles(const json& params);
    static json grepFiles(const json& params);

    // Security helpers
    static bool isPathAllowed(const std::string& path);
    static bool isPatternBlocked(const std::string& path);
    static std::string canonicalizePath(const std::string& path);

    // Error helpers
    static json makeError(const std::string& message);
    static json makeSuccess(const json& content);

    // Configuration (static for simplicity)
    static std::vector<std::string> s_allowedPaths;
    static std::vector<std::string> s_blockedPatterns;
    static size_t s_maxReadSize;
    static size_t s_maxWriteSize;
};

} // namespace celuna::tools
