package com.atlassian.bitbucket.mesh.git.hook;

import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilderSupport;
import com.atlassian.bitbucket.mesh.git.hosting.HostingCacheResult;
import com.atlassian.bitbucket.mesh.git.hosting.HostingOperationType;
import com.atlassian.bitbucket.mesh.repository.Repository;

import javax.annotation.Nonnull;
import java.io.Closeable;
import java.util.Optional;

/**
 * A handle on the hook request which provides the required information to make a successful callback
 * to Stash via their hook callback socket
 */
public interface HookRequestHandle extends Closeable {

    <B extends GitProcessBuilderSupport<B>> B configure(@Nonnull B builder);

    /**
     * Indicate that the SCM provider has finished with the hook request. This allows the host application to
     * cleanup any references it is maintaining
     */
    @Override
    void close();

    /**
     * @return the cache result, if caching was applied, or {@code empty()} if not
     */
    @Nonnull
    Optional<HostingCacheResult> getCacheResult();

    /**
     * @return the {@code --filter}, if one was applied, or {@code empty()} if not
     */
    @Nonnull
    Optional<String> getFilter();

    /**
     * @return the operation type, if the hook could determine it, or {@code empty()} if not
     */
    @Nonnull
    Optional<HostingOperationType> getOperationType();

    /**
     * @return the repository for which the hook handle is registered.
     */
    @Nonnull
    Repository getRepository();

    /**
     * @return the request id to identify the callback. This is securely generated
     */
    @Nonnull
    String getRequestId();

    /**
     * @return whether the hook request was accepted
     */
    boolean isAccepted();

    /**
     * @return whether any hooks have called back into the server
     */
    boolean isCalled();
}
