package service

import (
	"context"
	"time"

	"github.com/pingcap-inc/it-automation/internal/sync-google-email/store"
	"github.com/pingcap-inc/it-automation/pkg/log"
	"github.com/pingcap-inc/it-automation/pkg/storage"
)

type SgeSrv interface {
	Sync()
	DeptCompare(ctx context.Context)
	CheckRegionEmail(ctx context.Context)
	GroupOpen()
	CheckGoogleUsers(ctx context.Context)
	SyncDeptUser()
}

type sgeService struct {
	store store.Factory
}

var _ SgeSrv = (*sgeService)(nil)

func newSgeEmail(str *service) *sgeService {
	return &sgeService{store: str.store}
}

func (s *sgeService) Sync() {
	ctx := context.Background()
	var timeout = time.Second * 10
	tag := false
	for {
		if tag {
			break
		}
		isRedisOk := storage.Connected()
		if isRedisOk {
			break
		}
		time.Sleep(time.Second * 1)
		select {
		case <-time.After(timeout):
			tag = true
		default:
			log.L(ctx).Info("redis is not connected")
		}
	}
	log.Infof("redis is already connected")

	authCompare(ctx)
}
