package com.atlassian.bitbucket.internal.mirroring.mirror;

import com.atlassian.bitbucket.IntegrityException;
import com.atlassian.bitbucket.mirroring.mirror.UpstreamServer;
import com.atlassian.bitbucket.user.EscalatedSecurityContext;
import com.atlassian.bitbucket.user.ServiceUser;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * Helps retrieve service users for {@link UpstreamServer the upstream server}
 */
public interface UpstreamUserHelper {

    /**
     * If a service user is not already associated with the identified upstream server, one is created for it (in the
     * active state) otherwise the existing user is made active if not already active.
     *
     * Note: no checking is done for the validity of the upstream id.
     */
    @Nonnull
    ServiceUser createNewOrActivateExistingUserForUpstream(@Nonnull String upstreamId);

    /**
     * @return the newly created service user for the upstream identified
     * @throws IntegrityException if a service user already exists for this upstream server
     */
    @Nonnull
    ServiceUser createUserForUpstream(@Nonnull String upstreamId);

    /**
     * Disables the service user associated with the identified upstream if one exists.
     * @return the deactivated user or null if one does not exist
     */
    @Nullable
    ServiceUser deactivateUserForUpstream(@Nonnull String upstreamId);

    /**
     * @return the service user for the identified upstream if one exists.
     */
    @Nullable
    ServiceUser getUserForUpstream(@Nonnull String upstreamId);

    /**
     * @return an escalated security context for the service user associated with the identified
     * upstream server or null if no such user can be found
     */
    @Nullable
    EscalatedSecurityContext asUserForUpstream(@Nonnull String upstreamId);
}
