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

import com.atlassian.bitbucket.repository.Repository;

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

/**
 * Generates or retrieves a content hash that identifies the current contents of the repository. This content hash is
 * stable as long are no changes to the repository. When a ref in the repository changes, the hash will change.
 */
public interface RepositoryContentHashService {

    /**
     * Generates a new hash then persists and returns it
     *
     * @param repository the repository
     * @return the hash that identifies the current contents of the repository. If it cannot be computed (due to
     *         exception or because the repository SCM type is not supported) an {@link Optional#empty() empty} value
     *         is returned.
     */
    @Nonnull
    Optional<String> generateAndSetHash(Repository repository);

    /**
     * @param repository the repository
     * @return the hash that identifies the current contents of the repository. If it cannot be computed (due to
     *         exception or because the repository SCM type is not supported) an {@link Optional#empty() empty} value
     *         is returned.
     */
    @Nonnull
    Optional<String> getHash(@Nonnull Repository repository);

    /**
     * Updates the repository's hash with the provided {@code hash} value. This method should only be called on a
     * mirror instance after it has updated its contents.
     *
     * @param repository the repository
     * @param hash the hash to set
     */
    void setHash(@Nonnull Repository repository, @Nonnull String hash);
}
