package workers

import (
	"time"

	"github.com/pingcap/dbaas/billing2/config"
	"github.com/pingcap/dbaas/billing2/model/billing"
	"github.com/pingcap/log"
	"github.com/spf13/viper"
	"go.uber.org/zap"
)

func RemoveUnusedCosts() {
	measureModel := &billing.MeasureModel{}
	now := getNowDate().Add(-viper.GetDuration(config.RemoveUnusedCostsDays) * time.Hour * 24)
	err := measureModel.RemoveUnusedCosts(now, now.Add(time.Hour*24))
	if err != nil {
		log.Error("failed to remove unused costs", zap.Error(err))
	}
	log.Info("remove all unused costs", zap.Time("now", now))
}

func MergeSmallCosts() {
	measureModel := &billing.MeasureModel{}
	now := getNowDate().Add(-viper.GetDuration(config.MergeSmallCostsDays) * time.Hour * 24)
	err := measureModel.MergeSmallUnits(now, now.Add(time.Hour*24))
	if err != nil {
		log.Error("failed to remove unused costs", zap.Error(err))
	}
	log.Info("merged all small costs", zap.Time("now", now))
}
