package control

import (
	"context"

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

// GetNetmapStatus gets node status in FrostFS network.
func (s *Server) GetNetmapStatus(_ context.Context, req *control.GetNetmapStatusRequest) (*control.GetNetmapStatusResponse, error) {
	if err := s.isValidRequest(req); err != nil {
		return nil, status.Error(codes.PermissionDenied, err.Error())
	}

	st, epoch, err := s.nodeState.GetNetmapStatus()
	if err != nil {
		return nil, err
	}

	resp := &control.GetNetmapStatusResponse{
		Body: &control.GetNetmapStatusResponse_Body{
			Status: st,
			Epoch:  epoch,
		},
	}

	if err := ctrlmessage.Sign(s.key, resp); err != nil {
		return nil, status.Error(codes.Internal, err.Error())
	}

	return resp, nil
}