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

import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;
import com.atlassian.bitbucket.mesh.git.revert.RevertParameters;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcRevertInvalidParentNumber;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

/**
 * An exception that is raised when the {@link GitProcessBuilder#revert() revert} operation fails due to invalid
 * {@link RevertParameters#getMergedCommitParentNumber() merged commit parent number}.
 *
 * @since 2.1
 */
public class InvalidParentNumberException extends RevertException {

    private final int parentNumber;

    public InvalidParentNumberException(@Nonnull String commitId, int parentNumber) {
        super("The revert could not be completed as the commit " + commitId + " does not have parent " + parentNumber,
                requireNonNull(commitId, "commitId"));
        this.parentNumber = parentNumber;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setRevertInvalidParentNumber(RpcRevertInvalidParentNumber.newBuilder()
                        .setCommitId(commitId)
                        .setMergedCommitParentNumber(parentNumber)
                        .build())
                .build();
    }
}
