// Code generated by candi v1.8.17.

package grpchandler

import (
	"context"
	"time"

	proto "monorepo/sdk/user-service/proto/member"
	"monorepo/services/user-service/internal/modules/member/usecase"

	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"

	"pkg.agungdp.dev/candi/codebase/factory/types"
	"pkg.agungdp.dev/candi/codebase/interfaces"
	"pkg.agungdp.dev/candi/tracer"
)

// GRPCHandler rpc handler
type GRPCHandler struct {
	mw        interfaces.Middleware
	uc        usecase.MemberUsecase
	validator interfaces.Validator
}

// NewGRPCHandler func
func NewGRPCHandler(mw interfaces.Middleware, uc usecase.MemberUsecase, validator interfaces.Validator) *GRPCHandler {
	return &GRPCHandler{
		mw: mw, uc: uc, validator: validator,
	}
}

// Register grpc server
func (h *GRPCHandler) Register(server *grpc.Server, mwGroup *types.MiddlewareGroup) {
	proto.RegisterMemberHandlerServer(server, h)

	// register middleware for method
	mwGroup.AddProto(proto.File_member_member_proto, "Hello", h.mw.GRPCBasicAuth)
}

// GetMember rpc method
func (h *GRPCHandler) GetMember(ctx context.Context, req *proto.GetMemberRequest) (*proto.GetMemberResponse, error) {
	trace := tracer.StartTrace(ctx, "MemberDeliveryGRPC:GetMember")
	defer trace.Finish()
	ctx = trace.Context()

	member, err := h.uc.GetDetailMember(ctx, req.ID)
	if err != nil {
		return nil, grpc.Errorf(codes.NotFound, err.Error())
	}

	return &proto.GetMemberResponse{
		ID:         member.ID,
		Fullname:   member.Fullname,
		Username:   member.Username,
		Password:   member.Password,
		CreatedAt:  member.CreatedAt.Format(time.RFC3339),
		ModifiedAt: member.ModifiedAt.Format(time.RFC3339),
	}, nil
}
