package com.emonster.taroaichat.service.llm.openrouter.tools;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.Map;

/**
 * Abstract base class for AI function calling tools.
 * Provides a foundation for extensible tool system that can be used
 * with OpenAI-compatible function calling APIs.
 */
public abstract class AITool {

    protected final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * Get the unique name of this tool.
     * This will be used as the function name in API calls.
     */
    public abstract String getName();

    /**
     * Get a description of what this tool does.
     * This helps the AI understand when to use this tool.
     */
    public abstract String getDescription();

    /**
     * Get the JSON schema for this tool's parameters.
     * Should follow OpenAI function calling parameter schema format.
     */
    public abstract JsonNode getParameterSchema();

    /**
     * Execute the tool with the given parameters.
     * This is for future server-side execution of tools.
     *
     * @param parameters The parameters passed by the AI
     * @return Result of the tool execution
     */
    public abstract ToolResult execute(Map<String, Object> parameters);

    /**
     * Convert this tool to the format expected by OpenAI-compatible APIs.
     */
    public Map<String, Object> toApiFormat() {
        ObjectNode tool = objectMapper.createObjectNode();
        tool.put("type", "function");

        ObjectNode function = objectMapper.createObjectNode();
        function.put("name", getName());
        function.put("description", getDescription());
        function.set("parameters", getParameterSchema());

        tool.set("function", function);
        return objectMapper.convertValue(tool, new com.fasterxml.jackson.core.type.TypeReference<Map<String, Object>>() {});
    }

    /**
     * Validate the provided parameters against the schema.
     * Can be overridden for custom validation logic.
     */
    public boolean validateParameters(Map<String, Object> parameters) {
        // Basic validation - check required fields exist
        // Subclasses can override for more complex validation
        return parameters != null;
    }

    /**
     * Result of tool execution.
     */
    public static class ToolResult {
        private final boolean success;
        private final String message;
        private final Map<String, Object> data;

        public ToolResult(boolean success, String message, Map<String, Object> data) {
            this.success = success;
            this.message = message;
            this.data = data;
        }

        // Static factory methods for convenience
        public static ToolResult success(String message, Map<String, Object> data) {
            return new ToolResult(true, message, data);
        }

        public static ToolResult success(String message) {
            return new ToolResult(true, message, null);
        }

        public static ToolResult failure(String message) {
            return new ToolResult(false, message, null);
        }

        // Getters
        public boolean isSuccess() {
            return success;
        }

        public String getMessage() {
            return message;
        }

        public Map<String, Object> getData() {
            return data;
        }
    }
}
