/*
 * 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.context;

import org.apache.flink.agents.api.Event;
import org.apache.flink.agents.api.configuration.ReadableConfiguration;
import org.apache.flink.agents.api.memory.BaseLongTermMemory;
import org.apache.flink.agents.api.metrics.FlinkAgentsMetricGroup;
import org.apache.flink.agents.api.resource.Resource;
import org.apache.flink.agents.api.resource.ResourceType;

import java.util.Map;

/**
 * A context object used during action execution. It is responsible for collecting output events
 * generated by the action and providing access to state.
 */
public interface RunnerContext {
    /**
     * Sends an event.
     *
     * @param event the event to be sent
     */
    void sendEvent(Event event);

    /**
     * Gets the sensory memory.
     *
     * <p>Sensory memory is similar to short-term memory, but will be auto cleared after agent run
     * finished. User could use it to store data that does not need to be shared across agent runs.
     *
     * @return MemoryObject the root of the sensory memory
     * @throws Exception if the underlying state backend cannot be accessed
     */
    MemoryObject getSensoryMemory() throws Exception;

    /**
     * Gets the short-term memory.
     *
     * @return MemoryObject the root of the short-term memory
     * @throws Exception if the underlying state backend cannot be accessed
     */
    MemoryObject getShortTermMemory() throws Exception;

    /**
     * Gets the long-term memory.
     *
     * @return The long-term memory instance
     */
    BaseLongTermMemory getLongTermMemory() throws Exception;

    /**
     * Gets the metric group for Flink Agents.
     *
     * <p>The returned group must only be accessed from the operator/mailbox (action) thread, not
     * from inside a {@link #durableExecute} or {@link #durableExecuteAsync} callable, which runs on
     * a separate thread pool.
     *
     * @return the metric group shared across all actions.
     */
    FlinkAgentsMetricGroup getAgentMetricGroup();

    /**
     * Gets the individual metric group dedicated for each action.
     *
     * <p>The returned group must only be accessed from the operator/mailbox (action) thread, not
     * from inside a {@link #durableExecute} or {@link #durableExecuteAsync} callable, which runs on
     * a separate thread pool.
     *
     * @return the individual metric group specific to the current action.
     */
    FlinkAgentsMetricGroup getActionMetricGroup();

    /**
     * Gets a resource by name and type.
     *
     * @param name the resource name
     * @param type the resource type
     * @return the resource instance
     * @throws Exception if the resource cannot be found or created
     */
    Resource getResource(String name, ResourceType type) throws Exception;

    /**
     * Gets the configuration for Flink Agents.
     *
     * @return the configuration for Flink Agents.
     */
    ReadableConfiguration getConfig();

    /**
     * Get the configuration of the action.
     *
     * @return the configuration of the action.
     */
    Map<String, Object> getActionConfig();

    /**
     * Get the option value of the action config.
     *
     * @return the option value of the action config.
     */
    Object getActionConfigValue(String key);

    /**
     * Synchronously executes the provided callable with durable execution support.
     *
     * <p>The result will be stored and returned from cache during job recovery. The callable is
     * executed synchronously, blocking the operator until completion.
     *
     * <p>If the callable provides a reconcile callable via {@link DurableCallable#reconciler()},
     * recovery may invoke it for an in-flight durable call.
     *
     * <p>Access to memory and sendEvent are prohibited within the callable.
     */
    <T> T durableExecute(DurableCallable<T> callable) throws Exception;

    /**
     * Asynchronously executes the provided callable with durable execution support.
     *
     * <p>On JDK 21+, this method uses Continuation to yield the current action execution, submits
     * the callable to a thread pool, and resumes when complete. On JDK &lt; 21, this falls back to
     * synchronous execution.
     *
     * <p>The result will be stored and returned from cache during job recovery.
     *
     * <p>If the callable provides a reconcile callable via {@link DurableCallable#reconciler()},
     * recovery may invoke it for an in-flight durable call.
     *
     * <p>Access to memory and sendEvent are prohibited within the callable.
     */
    <T> T durableExecuteAsync(DurableCallable<T> callable) throws Exception;

    /** Clean up the resource. */
    void close() throws Exception;
}
