/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.agents.api.tools;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.flink.agents.api.resource.ResourceType;
import org.apache.flink.agents.api.resource.SerializableResource;

import java.lang.reflect.Method;

/**
 * Simplified base class for all tools. Focus on essential tool functionality without
 * overcomplicated features.
 */
public abstract class Tool extends SerializableResource {

    protected ToolMetadata metadata;

    protected Tool(ToolMetadata metadata) {
        this.metadata = java.util.Objects.requireNonNull(metadata, "metadata cannot be null");
    }

    /**
     * Replace this tool's metadata. Intended for subclasses that derive metadata lazily once a
     * runtime bridge becomes available (e.g. {@code FunctionTool} backed by a {@code
     * PythonFunction} refreshing placeholder metadata via the JVM&rarr;Python adapter).
     */
    protected void setMetadata(ToolMetadata metadata) {
        this.metadata = java.util.Objects.requireNonNull(metadata, "metadata cannot be null");
    }

    /** Get the metadata of this tool. */
    public final ToolMetadata getMetadata() {
        return metadata;
    }

    /** Return resource type of class. */
    @JsonIgnore
    @Override
    public final ResourceType getResourceType() {
        return ResourceType.TOOL;
    }

    /** Return tool type of class. */
    public abstract ToolType getToolType();

    /** Call the tool with parameters. This is the core method for tool execution. */
    public abstract ToolResponse call(ToolParameters parameters);

    /** Get the tool name from metadata. */
    @JsonIgnore
    public final String getName() {
        return metadata.getName();
    }

    /** Get the tool description from metadata. */
    @JsonIgnore
    public final String getDescription() {
        return metadata.getDescription();
    }

    /** Get tool keeps a method. */
    public static FunctionTool fromMethod(Method method) {
        return FunctionTool.fromMethod(method);
    }
}
