package com.atlassian.bitbucket.scm.cache;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Date;
import java.util.Set;

/**
 * Service to configure the SCM cache.
 *
 * @since 2.0.0
 */
public interface ScmCacheConfig {
    /**
     * Convenience method for constructing an expiry date for a given request type.
     * @param   requestType the request type
     * @return  {@code null} if no time to live has been configured for the provided request type. Otherwise a
     *          {@link java.util.Date} that is ttl in the future from now.
     */
    @Nullable
    Date createExpiryDate(@Nonnull ScmRequestType requestType);

    /**
     * @return the protocols for which caching has been enabled
     */
    @Nonnull
    Set<ScmRequestProtocol> getEnabledProtocols();

    /**
     * @param requestType the request type
     * @return the maximum number of cache entries to retain (per repository) for the given request type
     */
    int getMaxCount(@Nonnull ScmRequestType requestType);

    /**
     * @param requestType the request type
     * @return the time to live in seconds for the provided request type. {@code -1} if caches for this request
     *         type don't expire.
     */
    long getTimeToLive(@Nonnull ScmRequestType requestType);

    /**
     * @param requestType the request type
     * @return whether caching is enabled for the provided request type.
     */
    boolean isEnabled(@Nonnull ScmRequestType requestType);

    /**
     * @param protocol the protocol
     * @return whether caching is enabled for the provided protocol
     */
    boolean isEnabled(@Nonnull ScmRequestProtocol protocol);

    /**
     * Enables or disables caching for the provided type
     * @param requestType the request type
     * @param enabled {@code true} if caching should be enabled
     */
    void setEnabled(@Nonnull ScmRequestType requestType, boolean enabled);

    /**
     * Enables or disables caching for the provided protocol
     * @param protocol the request protocol
     * @param enabled {@code true} if caching should be enabled
     */
    void setEnabled(@Nonnull ScmRequestProtocol protocol, boolean enabled);

    /**
     * @param requestType the request type
     * @param maxCount the maximum number of cache entries to retain (per repository) for the given request type
     */
    void setMaxCount(@Nonnull ScmRequestType requestType, int maxCount);

    /**
     * @param requestType the request type
     * @param timeToLiveSeconds the time to live in seconds. A value <= 0 disables TTL based cache expiry
     */
    void setTimeToLive(@Nonnull ScmRequestType requestType, long timeToLiveSeconds);
}
