package sso

import (
	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
	"gitee.com/xuyiping_admin/pkg/di"
	"kpt-pasture/config"

	redisv7 "github.com/go-redis/redis/v7"
)

var Module = di.Provide(NewSSOClient)

type ClientInterface interface {
	Auth(userAuth *operationPb.UserAuthData) (string, error)
	CacheAuth(token string, res interface{}) error
	CacheSetAccount(token string, res interface{}) error
	GetAccount(token string) (interface{}, error)
	// Permissions(token string) (*Response, error)
	// CheckPermission(token, code string) (bool, error)
}

func NewClientLatest(cfg *config.AppConfig) *redisv7.Client {
	return initClientLatest(cfg.RedisSetting.SSOCache.Addr, cfg.RedisSetting.SSOCache.Requirepass, cfg.RedisSetting.SSOCache.DB)
}

func initClientLatest(addr, password string, db int, opts ...func(*redisv7.Options)) *redisv7.Client {
	option := &redisv7.Options{
		Addr:       addr,
		DB:         db,
		Password:   password,
		MaxRetries: 3,
		OnConnect: func(cn *redisv7.Conn) error {
			return cn.ClientSetName("on_connect").Err()
		},
	}
	for _, opt := range opts {
		opt(option)
	}

	return redisv7.NewClient(option)
}

func NewSSOClient(cfg *config.AppConfig) ClientInterface {
	return NewCache(cfg)
}