package service

import (
	"fmt"

	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/context"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/log"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/module"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/service/hook"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/service/rank"
)

type UserRecallService struct {
	RecommendService
	recallService *RecallService
	rankService   *rank.RankService
	// userDao       module.UserDao
	// itemDao       module.ItemDao
}

func NewUserRecallService() *UserRecallService {
	service := UserRecallService{
		recallService: &RecallService{},
		// rankService:   rank.NewRankService(),
		// userDao: module.NewUserDao(recconf.Config.UserDaoConf.Adapter),
		// itemDao: module.NewItemDao(recconf.Config.ItemDaoConf.Adapter),
	}
	return &service
}
func (r *UserRecallService) Recommend(context *context.RecommendContext) []*module.Item {
	userId := r.GetUID(context)
	user := module.NewUserWithContext(userId, context)
	items := r.recallService.GetItems(user, context)

	// filter
	items = r.Filter(userId, items, context)

	size := context.Size

	if len(items) < size {
		log.Info(fmt.Sprintf("requestId=%s\tmodule=recommend\tevent=filter\tmsg=length of items less than size\tcount=%d", context.RecommendId, len(items)))
		return items
	}

	items = items[:size]

	// asynchronous clean hook func
	for _, hf := range hook.RecommendCleanHooks {
		go hf(context, user, items)
	}
	return items
}
