package sort

import (
	"errors"

	"gitlab.alibaba-inc.com/pai_biz_arch/pairec/module"
	psort "gitlab.alibaba-inc.com/pai_biz_arch/pairec/sort"
)

type VideoMixSort struct {
}

func NewVideoMixSort() *VideoMixSort {
	return &VideoMixSort{}
}
func (s *VideoMixSort) Sort(sortData *psort.SortData) error {
	if _, ok := sortData.Data.([]*module.Item); !ok {
		return errors.New("sort data type error")

	}
	return s.doSort(sortData)
}

// 输出混排，冷启动召回保留 1/10 量，保证新物品透出
func (s *VideoMixSort) doSort(sortData *psort.SortData) error {
	items := sortData.Data.([]*module.Item)
	size := sortData.Context.Size
	coldRecallSize := size / 10
	if coldRecallSize < 1 {
		coldRecallSize = 1
	}
	otherSize := size - coldRecallSize

	container := psort.NewItemContaienr(psort.SplitByRecallName, size)
	container.Assign("ColdStartRecall", coldRecallSize, size, psort.SortByRandomStrategy)
	container.Assign(psort.ItemDefaultName, otherSize, size, psort.SortByScoreStrategy)
	container.Split(items)

	newItems := container.Assembly()
	sortData.Data = newItems

	return nil
}
