// Code generated by candi v1.4.0.

package workerhandler

import (
	"context"
	"encoding/json"
	"time"

	"monorepo/services/user-service/internal/modules/member/usecase"
	"monorepo/services/user-service/pkg/shared"
	"monorepo/services/user-service/pkg/shared/domain"

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

// TaskQueueHandler struct
type TaskQueueHandler struct {
	uc        usecase.MemberUsecase
	validator interfaces.Validator
}

// NewTaskQueueHandler constructor
func NewTaskQueueHandler(uc usecase.MemberUsecase, validator interfaces.Validator) *TaskQueueHandler {
	return &TaskQueueHandler{
		uc:        uc,
		validator: validator,
	}
}

// MountHandlers mount handler group
func (h *TaskQueueHandler) MountHandlers(group *types.WorkerHandlerGroup) {
	group.Add(shared.GetEnv().TaskAddMember, h.taskAddMember)
}

func (h *TaskQueueHandler) taskAddMember(ctx context.Context, message []byte) error {
	trace := tracer.StartTrace(ctx, "MemberDeliveryTaskQueue:TaskAddMember")
	defer trace.Finish()
	ctx = trace.Context()

	var member domain.Member
	if err := json.Unmarshal(message, &member); err != nil {
		return err
	}

	if err := h.uc.SaveMember(ctx, &member); err != nil {
		return &candishared.ErrorRetrier{
			Delay:   10 * time.Second,
			Message: err.Error(),
		}
	}
	return nil
}
