package queue

import (
	"context"
	"fmt"
	"ycc-server/app/main/api/internal/svc"
	"ycc-server/app/main/api/internal/types"

	"github.com/hibiken/asynq"
)

type CronJob struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCronJob(ctx context.Context, svcCtx *svc.ServiceContext) *CronJob {
	return &CronJob{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CronJob) Register() *asynq.ServeMux {
	redisClientOpt := asynq.RedisClientOpt{Addr: l.svcCtx.Config.CacheRedis[0].Host, Password: l.svcCtx.Config.CacheRedis[0].Pass}
	scheduler := asynq.NewScheduler(redisClientOpt, nil)
	task := asynq.NewTask(types.MsgCleanQueryData, nil, nil)
	_, err := scheduler.Register(TASKTIME, task)
	if err != nil {
		panic(fmt.Sprintf("定时任务注册失败:%v", err))
	}
	scheduler.Start()
	fmt.Println("定时任务启动！！！")

	mux := asynq.NewServeMux()
	mux.Handle(types.MsgPaySuccessQuery, NewPaySuccessNotifyUserHandler(l.svcCtx))
	mux.Handle(types.MsgCleanQueryData, NewCleanQueryDataHandler(l.svcCtx))

	return mux
}
