package private_link

import (
	"context"
	"time"

	"github.com/go-co-op/gocron"
	"github.com/tidbcloud/dataflow-service/app/worker/common"
)

const (
	privateLinkStatusIntervalSeconds                = 30
	privateLinkGCIntervalMinutes                    = 5
	syncPrivateLinkChangefeedTrafficIntervalMinutes = 5
	privateLinkChangefeedBillingIntervalMinutes     = 60
	cleanupUnusedEndpointsIntervalMinutes           = 60
)

func NewWorkerScheduler(ctx context.Context, loc *time.Location) (*gocron.Scheduler, error) {
	s := gocron.NewScheduler(loc)
	s.SingletonModeAll()

	if _, err := s.Every(privateLinkChangefeedBillingIntervalMinutes).Minutes().
		Do(common.WorkerFunWrapper("BillPrivateLinkWorker", BillPrivateLinkWorker)); err != nil {
		return nil, err
	}
	if _, err := s.Every(syncPrivateLinkChangefeedTrafficIntervalMinutes).Minutes().
		Do(common.WorkerFunWrapper("MeterPrivateLinkWorker", MeterPrivateLinkWorker)); err != nil {
		return nil, err
	}
	if _, err := s.Every(privateLinkStatusIntervalSeconds).Seconds().
		Do(common.WorkerFunWrapper("SyncSinkEndpointWorker", SyncSinkEndpointWorker)); err != nil {
		return nil, err
	}
	if _, err := s.Every(cleanupUnusedEndpointsIntervalMinutes).Minutes().
		Do(common.WorkerFunWrapper("CleanupUnusedEndpointsWorker", CleanupUnusedEndpointsWorker)); err != nil {
		return nil, err
	}
	return s, nil
}
