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

import com.google.genai.types.FunctionDeclaration;
import com.google.genai.types.Schema;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Abstract base class for Gemini-native AI tools.
 * Follows the official Google Gemini SDK patterns for function calling.
 */
public abstract class GeminiAITool {
    
    protected final Logger LOG = LoggerFactory.getLogger(getClass());
    
    /**
     * Get the unique name of this tool.
     * This will be used as the function name in Gemini 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 Gemini SDK Schema for this tool's parameters.
     * Should follow Gemini function calling parameter schema format.
     */
    public abstract Schema getParameterSchema();
    
    /**
     * Execute the tool with the given parameters.
     * 
     * @param parameters The parameters passed by the AI
     * @return Result of the tool execution
     */
    public abstract GeminiToolResult execute(Map<String, Object> parameters);
    
    /**
     * Convert this tool to Gemini FunctionDeclaration format.
     */
    public FunctionDeclaration toFunctionDeclaration() {
        return FunctionDeclaration.builder()
            .name(getName())
            .description(getDescription())
            .parameters(getParameterSchema())
            .build();
    }
    
    /**
     * Validate the provided parameters.
     * Can be overridden for custom validation logic.
     */
    public boolean validateParameters(Map<String, Object> parameters) {
        // Basic validation - check parameters exist
        // Subclasses can override for more complex validation
        return parameters != null;
    }
    
    /**
     * Result of Gemini tool execution.
     */
    public static class GeminiToolResult {
        private final boolean success;
        private final String message;
        private final Map<String, Object> data;
        
        public GeminiToolResult(boolean success, String message, Map<String, Object> data) {
            this.success = success;
            this.message = message;
            this.data = data;
        }
        
        // Static factory methods for convenience
        public static GeminiToolResult success(String message, Map<String, Object> data) {
            return new GeminiToolResult(true, message, data);
        }
        
        public static GeminiToolResult success(String message) {
            return new GeminiToolResult(true, message, null);
        }
        
        public static GeminiToolResult failure(String message) {
            return new GeminiToolResult(false, message, null);
        }
        
        // Getters
        public boolean isSuccess() {
            return success;
        }
        
        public String getMessage() {
            return message;
        }
        
        public Map<String, Object> getData() {
            return data;
        }
    }
}