package com.atlassian.bitbucket.mesh.repository;

import com.atlassian.bitbucket.mesh.AbstractStatusException;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcErrorUtils;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcRepositoryAlreadyExists;
import io.grpc.Metadata;
import io.grpc.Status;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.nio.file.Path;

import static java.util.Objects.requireNonNull;

public class RepositoryAlreadyExistsException extends AbstractStatusException {

    private final String id;
    private final Path path;

    public RepositoryAlreadyExistsException(@Nonnull String id, @Nonnull Path path, @Nullable String message) {
        super(message, Status.ALREADY_EXISTS);

        this.id = requireNonNull(id, "id");
        this.path = requireNonNull(path, "path");
    }

    @Nonnull
    public String getId() {
        return id;
    }

    @Override
    protected Metadata getTrailers() {
        return RpcErrorUtils.toMetadata(RpcError.newBuilder()
                .setRepositoryAlreadyExists(RpcRepositoryAlreadyExists.newBuilder()
                        .setRepository(id)
                        .setPath(path.toString()))
                .build());
    }
}
