package com.atlassian.bitbucket.mesh.repository;

import com.atlassian.bitbucket.mesh.AbstractStatusException;
import com.atlassian.bitbucket.mesh.rpc.v1.*;
import io.grpc.Metadata;
import io.grpc.Status;

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

import static java.util.Objects.requireNonNull;

public class ResetHierarchyFailedException extends AbstractStatusException {

    private final String hierarchyId;

    public ResetHierarchyFailedException(@Nonnull String hierarchyId) {
        this(hierarchyId, null);
    }

    public ResetHierarchyFailedException(@Nonnull String hierarchyId, @Nullable Throwable cause) {
        super("Failed to reset hierarchy " + requireNonNull(hierarchyId, "hierarchyId"), cause, Status.INTERNAL);

        this.hierarchyId = hierarchyId;
    }

    @Nonnull
    public String getHierarchyId() {
        return hierarchyId;
    }

    @Override
    protected Metadata getTrailers() {
        return RpcErrorUtils.toMetadata(RpcError.newBuilder()
                .setResetHierarchyFailed(RpcResetHierarchyFailed.newBuilder()
                        .setHierarchy(hierarchyId))
                .build());
    }
}
