// Code generated by candi v1.8.17.

package workerhandler

import (
	"context"
	"encoding/json"

	"monorepo/services/auth-service/internal/modules/token/domain"
	"monorepo/services/auth-service/internal/modules/token/usecase"

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

// RedisHandler struct
type RedisHandler struct {
	uc        usecase.TokenUsecase
	validator interfaces.Validator
}

// NewRedisHandler constructor
func NewRedisHandler(uc usecase.TokenUsecase, validator interfaces.Validator) *RedisHandler {
	return &RedisHandler{
		uc:        uc,
		validator: validator,
	}
}

// MountHandlers mount handler group
func (h *RedisHandler) MountHandlers(group *types.WorkerHandlerGroup) {
	group.Add(domain.RedisTokenExpiredKeyConst, h.handleTokenExpired)
}

func (h *RedisHandler) handleTokenExpired(ctx context.Context, message []byte) error {
	trace := tracer.StartTrace(ctx, "TokenDeliveryRedis:HandleTokenExpired")
	defer trace.Finish()
	ctx = trace.Context()

	var data domain.RedisTokenExpiredKey
	json.Unmarshal(message, &data)
	return h.uc.RevokeByKey(ctx, data.DeviceID, data.UserID)
}
