package control

import (
	"context"

	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/engine"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

func (s *Server) Doctor(ctx context.Context, req *control.DoctorRequest) (*control.DoctorResponse, error) {
	err := s.isValidRequest(req)
	if err != nil {
		return nil, status.Error(codes.PermissionDenied, err.Error())
	}

	if !req.GetBody().GetRemoveDuplicates() {
		return nil, status.Error(codes.InvalidArgument, "operation not specified")
	}

	var prm engine.RemoveDuplicatesPrm
	prm.Concurrency = int(req.GetBody().GetConcurrency())

	err = s.s.RemoveDuplicates(ctx, prm)
	if err != nil {
		return nil, status.Error(codes.Internal, err.Error())
	}

	resp := &control.DoctorResponse{Body: &control.DoctorResponse_Body{}}

	err = SignMessage(s.key, resp)
	if err != nil {
		return nil, status.Error(codes.Internal, err.Error())
	}
	return resp, nil
}