package com.atlassian.stash.internal.pull.comment.drift;

import com.atlassian.stash.internal.pull.InternalPullRequest;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

/**
 * Dao for dealing with {@link InternalDriftRequest} instances.
 * 
 * @since 1.3
 */
public interface CommentDriftRequestDao  {

    /**
     * Persists a {@link InternalDriftRequest}. 
     * @param request the request to be persisted
     * @return the persisted request, with the id set.
     */
    @Nonnull
    InternalDriftRequest create(@Nonnull InternalDriftRequest request);

    /**
     * Deletes a list of provided {@link InternalDriftRequest} instances.
     * @param drifts the instances to be deleted.
     */
    void deleteAll(@Nonnull List<InternalDriftRequest> drifts);

    /**
     * @param id the request id
     * @return the request or {@code null} if no {@link InternalDriftRequest} exists with that id.
     */
    @Nullable
    InternalDriftRequest getById(@Nonnull Long id);

    /**
     * @return all {@link InternalDriftRequest} instances persisted in the database.
     */
    @Nonnull
    List<InternalDriftRequest> findAll();

    /**
     * @return all {@link InternalDriftRequest} instances for a given pull request global id.
     * @since 3.2
     */
    @Nonnull
    List<InternalDriftRequest> findByGlobalId(long globalId);

    /**
     * @param maxAttempts the maximum number of attempts, used to filter out {@link InternalDriftRequest requests},
     *                    and their pull requests, which should not be retried
     * @return all {@link InternalPullRequest pull requests} with at least one pending {@link InternalDriftRequest}
     *         which has not exceeded {@code maxAttempts}
     * @since 3.2
     */
    @Nonnull
    List<InternalPullRequest> findPendingPullRequests(int maxAttempts);

    /**
     * Updates the {@link InternalDriftRequest#getAttempts() attempts} count on the specified
     * {@link InternalDriftRequest request}.
     *
     * @param request  the request to modify
     * @param attempts the number of attempts to record on the request
     * @since 4.8
     */
    void updateAttempts(@Nonnull InternalDriftRequest request, int attempts);
}
