package netmap

import (
	"context"
	"crypto/ecdsa"

	"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/netmap"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/util"
)

type signService struct {
	sigSvc *util.SignService

	svc Server
}

func NewSignService(key *ecdsa.PrivateKey, svc Server) Server {
	return &signService{
		sigSvc: util.NewUnarySignService(key),
		svc:    svc,
	}
}

func (s *signService) LocalNodeInfo(
	ctx context.Context,
	req *netmap.LocalNodeInfoRequest,
) (*netmap.LocalNodeInfoResponse, error) {
	if err := s.sigSvc.VerifyRequest(req); err != nil {
		resp := new(netmap.LocalNodeInfoResponse)
		return resp, s.sigSvc.SignResponse(resp, err)
	}
	resp, err := util.EnsureNonNilResponse(s.svc.LocalNodeInfo(ctx, req))
	return resp, s.sigSvc.SignResponse(resp, err)
}

func (s *signService) NetworkInfo(ctx context.Context, req *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) {
	if err := s.sigSvc.VerifyRequest(req); err != nil {
		resp := new(netmap.NetworkInfoResponse)
		return resp, s.sigSvc.SignResponse(resp, err)
	}
	resp, err := util.EnsureNonNilResponse(s.svc.NetworkInfo(ctx, req))
	return resp, s.sigSvc.SignResponse(resp, err)
}

func (s *signService) Snapshot(ctx context.Context, req *netmap.SnapshotRequest) (*netmap.SnapshotResponse, error) {
	if err := s.sigSvc.VerifyRequest(req); err != nil {
		resp := new(netmap.SnapshotResponse)
		return resp, s.sigSvc.SignResponse(resp, err)
	}
	resp, err := util.EnsureNonNilResponse(s.svc.Snapshot(ctx, req))
	return resp, s.sigSvc.SignResponse(resp, err)
}