package filter

import (
	"errors"
	"fmt"
	"time"

	"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/recconf"
	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/utils"
)

// user exposure history filter
type User2ItemExposureFilter struct {
	user2ItemExposureDao module.User2ItemExposureDao
}

func NewUser2ItemExposureFilter(config recconf.FilterConfig) *User2ItemExposureFilter {
	filter := User2ItemExposureFilter{
		user2ItemExposureDao: module.NewUser2ItemExposureDao(config),
	}

	return &filter
}
func (f *User2ItemExposureFilter) Filter(filterData *FilterData) error {
	if _, ok := filterData.Data.([]*module.Item); !ok {
		return errors.New("filter data type error")

	}
	return f.doFilter(filterData)
}

func (f *User2ItemExposureFilter) doFilter(filterData *FilterData) error {
	start := time.Now()
	items := filterData.Data.([]*module.Item)

	newItems := f.user2ItemExposureDao.FilterByHistory(filterData.Uid, items)

	filterData.Data = newItems
	log.Info(fmt.Sprintf("requestId=%s\tevent=User2ItemExposureFilter\tcount=%d\tcost=%d", filterData.Context.RecommendId, len(newItems), utils.CostTime(start)))
	return nil
}
func (f *User2ItemExposureFilter) MatchTag(tag string) bool {
	// default filter, so filter all tag
	return true
}
