package scheduler

import (
	"time"

	"github.com/perfect-panel/server/pkg/logger"

	"github.com/hibiken/asynq"
	"github.com/perfect-panel/server/internal/svc"
	"github.com/perfect-panel/server/queue/types"
)

type Service struct {
	svc    *svc.ServiceContext
	server *asynq.Scheduler
}

func NewService(svc *svc.ServiceContext) *Service {
	return &Service{
		svc:    svc,
		server: initService(svc),
	}
}

func (m *Service) Start() {
	logger.Infof("start scheduler service")
	// schedule check subscription task: every 60 seconds
	checkTask := asynq.NewTask(types.SchedulerCheckSubscription, nil)
	if _, err := m.server.Register("@every 60s", checkTask); err != nil {
		logger.Errorf("register check subscription task failed: %s", err.Error())
	}
	//// schedule total server data task: every 5 minutes
	//totalServerDataTask := asynq.NewTask(types.SchedulerTotalServerData, nil)
	//if _, err := m.server.Register("@every 180s", totalServerDataTask); err != nil {
	//	logger.Errorf("register total server data task failed: %s", err.Error())
	//}
	// schedule reset traffic task: every day at 00:30
	resetTrafficTask := asynq.NewTask(types.SchedulerResetTraffic, nil)
	if _, err := m.server.Register("30 0 * * *", resetTrafficTask); err != nil {
		logger.Errorf("register reset traffic task failed: %s", err.Error())
	}

	// schedule traffic stat task: every day at 00:00
	trafficStatTask := asynq.NewTask(types.SchedulerTrafficStat, nil)
	if _, err := m.server.Register("0 0 * * *", trafficStatTask, asynq.MaxRetry(3)); err != nil {
		logger.Errorf("register traffic stat task failed: %s", err.Error())
	}

	// schedule update exchange rate task: every day at 01:00
	rateTask := asynq.NewTask(types.ForthwithQuotaTask, nil)
	if _, err := m.server.Register("0 1 * * *", rateTask, asynq.MaxRetry(3)); err != nil {
		logger.Errorf("register update exchange rate task failed: %s", err.Error())
	}

	// schedule Apple IAP reconcile: every 5 minutes (第二层补单)
	iapReconcileTask := asynq.NewTask(types.SchedulerIAPReconcile, nil)
	if _, err := m.server.Register("@every 5m", iapReconcileTask, asynq.MaxRetry(1)); err != nil {
		logger.Errorf("register iap reconcile task failed: %s", err.Error())
	}

	// schedule Apple IAP daily reconcile: every day at 02:00 (第三层日终对账)
	iapDailyTask := asynq.NewTask(types.SchedulerIAPDailyReconcile, nil)
	if _, err := m.server.Register("0 2 * * *", iapDailyTask, asynq.MaxRetry(1)); err != nil {
		logger.Errorf("register iap daily reconcile task failed: %s", err.Error())
	}

	// schedule stuck order recovery: every 10 minutes
	stuckOrderTask := asynq.NewTask(types.SchedulerStuckOrderRecovery, nil)
	if _, err := m.server.Register("@every 10m", stuckOrderTask, asynq.MaxRetry(1)); err != nil {
		logger.Errorf("register stuck order recovery task failed: %s", err.Error())
	}

	if err := m.server.Run(); err != nil {
		logger.Errorf("run scheduler failed: %s", err.Error())
	}
}

func (m *Service) Stop() {
	logger.Info("stop scheduler service")
	m.server.Shutdown()
}

func initService(svc *svc.ServiceContext) *asynq.Scheduler {
	location, _ := time.LoadLocation("Asia/Shanghai")
	return asynq.NewScheduler(
		asynq.RedisClientOpt{Addr: svc.Config.Redis.Host, Password: svc.Config.Redis.Pass, DB: 5},
		&asynq.SchedulerOpts{
			Location: location,
		},
	)
}
