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

import com.atlassian.bitbucket.mesh.git.exception.AbstractGitException;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcInvalidWorkTreePath;
import com.google.protobuf.ByteString;
import io.grpc.Status;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

public class InvalidWorkTreePathException extends AbstractGitException {

    private final String path;
    private final RpcInvalidWorkTreePath.Reason reason;

    public InvalidWorkTreePathException(@Nonnull String path, @Nonnull RpcInvalidWorkTreePath.Reason reason) {
        super("Invalid work tree path", Status.INVALID_ARGUMENT);
        this.path = requireNonNull(path, "path");
        this.reason = requireNonNull(reason, "reason");
    }

    @Nonnull
    public String getPath() {
        return path;
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setInvalidWorkTreePath(RpcInvalidWorkTreePath.newBuilder()
                        .setReason(reason)
                        .setPath(ByteString.copyFromUtf8(path)))
                .build();
    }
}
