package cron

import (
	"context"
	"kami/internal/service"

	"github.com/gogf/gf/v2/net/gtrace"
	"github.com/gogf/gf/v2/os/gcron"
	"github.com/gogf/gf/v2/os/glog"
	"go.opentelemetry.io/otel/trace"
)

// Register 注册定时任务
func Register(ctx context.Context) {
	registerMainTasks(ctx)

	//// 骆驼加油模块定时任务
	//registerCamelOilTasks(ctx)
}

func registerMainTasks(ctx context.Context) {
	//每日0时执行
	_, _ = gcron.AddSingleton(ctx, "0 0 0 * * ?", func(ctx context.Context) {
		tracer := gtrace.NewTracer("每日0时定时任务")
		ctx, span := tracer.Start(ctx, "每日0时定时任务", trace.WithNewRoot())
		defer span.End()
		glog.Info(ctx, "每日0时执行，重置苹果账户状态")
		if err := service.AppleAccount().CronResetStatus(ctx, nil); err != nil {
			glog.Error(ctx, "重置苹果账户状态失败", err)
		}
		glog.Info(ctx, "每日0时执行，重置充值账户状态")
		if err := service.CardRedeemAccount().ResetTodayAmountTotal(ctx); err != nil {
			glog.Error(ctx, "重置充值账户状态", err)
		}
		glog.Info(ctx, "每日0时执行，每日账户充值统计")
		if err := service.CardRedeemAccount().DailySummary(ctx); err != nil {
			glog.Error(ctx, "每日账户充值统计失败", err)
		}
	})
	_, _ = gcron.AddSingleton(ctx, "@every 1m", func(ctx context.Context) {
		//处理临时停止的订单
		_ = service.AppleAccount().CronHandleTmpStoppedList(ctx)
		_ = service.AppleOrder().CronFailedScheduleTask(ctx)
		//if err := tmall.NewClient().CronAuthTask(ctx); err != nil {
		//	glog.Error(ctx, "遍历天猫授权失败", err)
		//}
	})
	_, _ = gcron.AddSingleton(ctx, "@every 2m", func(ctx context.Context) {
		service.AppleAccount().CronHealthCheck(ctx)
	})
	// 苹果订单回调定时任务（每5分钟执行一次）
	_, _ = gcron.AddSingleton(ctx, "@every 1s", func(ctx context.Context) {
		_ = service.AppleOrder().CronProcessOrderWithPush(ctx)
	})
	_, _ = gcron.AddSingleton(ctx, "@every 3s", func(ctx context.Context) {
		_ = service.AppleOrder().CronCallbackPendingOrders(ctx)
	})
	//_, _ = gcron.AddSingleton(ctx, "@every 3m", func(ctx context.Context) {
	//	//处理大多数轮询订单
	//	_ = service.CardRedeemOrder().TriggerConsumeWithContext(ctx)
	//	_ = service.CardRedeemAccount().CheckAccountStatus(ctx, consts.CardRedeemAccountCategoryWalMart)
	//})

	//_, _ = gcron.AddSingleton(ctx, "@every 5s", func(ctx context.Context) {
	//	_ = service.CardRedeemOrder().TriggerConsumeWithContext(ctx)
	//	//service.CardRedeemCookie().CheckPaySuccess(ctx, nil)
	//})

	//// 京东支付状态监控任务（每10s执行一次）
	//_, _ = gcron.AddSingleton(ctx, "@every 30s", func(ctx context.Context) {
	//	tracer := gtrace.NewTracer("京东支付状态监控任务")
	//	ctx, span := tracer.Start(ctx, "京东支付状态监控任务", trace.WithNewRoot())
	//	defer span.End()
	//	if err := service.JdCookie().ReleaseExpiredJdOrders(ctx); err != nil {
	//		glog.Error(ctx, "释放过期京东订单失败", err)
	//	}
	//	if err := service.JdCookie().CleanupExpiredOrders(ctx); err != nil {
	//		glog.Error(ctx, "清理过期订单失败", err)
	//	}
	//	if err := service.JdCookie().BatchCheckPaymentStatus(ctx); err != nil {
	//		glog.Error(ctx, "京东支付状态监控任务失败", err)
	//	}
	//})
	//registerCamelOilTasks(ctx)
}

//// registerCamelOilTasks 注册骆驼加油模块的定时任务
//func registerCamelOilTasks(ctx context.Context) {
//	_, _ = gcron.AddSingleton(ctx, "@every 10s", func(ctx context.Context) {
//		_ = service.CamelOil().CronAccountPrefetchTask(ctx)
//		_ = service.CamelOil().CronVerifyCodeCheckTask(ctx)
//		_ = service.CamelOil().CronOrderPaymentCheckTask(ctx)
//		_ = service.CamelOil().ProcessPendingCallbacks(ctx)
//		_, _ = service.CamelOil().CronCleanExpiredPrefetchOrders(ctx)
//		_ = service.CamelOil().CronCardBindingTask(ctx)
//		_ = service.CamelOil().CronExpiredTokensCode(ctx)
//	}, "CamelOilAccountPrefetch")
//
//	_, _ = gcron.AddSingleton(ctx, "0 1 0 * * ?", func(ctx context.Context) {
//		_ = service.CamelOil().CronAccountDailyResetTask(ctx)
//	}, "CamelOilAccountDailyReset")
//
//	_, _ = gcron.AddSingleton(ctx, "@every 1s", func(ctx context.Context) {
//		_ = service.CamelOil().CronPrefetchOrderSupplementTask(ctx)
//	}, "CamelOilPrefetchOrderSupplementOrders")
//
//	glog.Info(ctx, "骆驼加油模块定时任务注册完成")
//}
