package main

import (
	"context"
	"log"

	"github.com/tidbcloud/tiinf/monitor/tilog"
	"go.uber.org/zap"

	"github.com/tidbcloud/account/idl/pbgen/proto/account"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func main() {
	//testReflect()
	listUsersByPage()

	//updateOrg()
	//getOrgById()
	//listMspCustomers()
	//listOrgs()
	//testJwt02()

	//CreateApiKey()
	//UpdateApiKey()
	//gateApiKey()
}

/*
	func testJwt(client acl.AccessServiceClient, ctx context.Context) {
		token := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlJEWTNNVUpGTUVReU9VVTBSa0U1TXpCRk5rRkRRVEl3UkRNMU0wSXpNelJHUVRrMVFUbEZRZyJ9.eyJodHRwczovL3RpZGJjbG91ZC5jb20vbG9naW5fcmVjZWl2ZWRfYXQiOjE2NzIxOTI1NDU3NTIsImVtYWlsIjoidG9uZy5zaGVuQHBpbmdjYXAuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImlzcyI6Imh0dHBzOi8vdGlkYi5hdXRoMC5jb20vIiwic3ViIjoiZ29vZ2xlLW9hdXRoMnwxMTI0MDE0Nzc3ODcyNjYzOTU0ODgiLCJhdWQiOiI2SVp0aENmbVJLSVBFblFTVDhhRGJ0TTdTR2RNbmlSbCIsImlhdCI6MTY3MjE5MjU0NSwiZXhwIjoxNjcyMjI4NTQ1LCJhdF9oYXNoIjoiWWg4RmpFZVdMLVJWZ2twWFRqa3doQSIsInNpZCI6IkM5MUxCMDlrVExtTXYtRnhfendISUJ6QnJwOWxraUUyIiwibm9uY2UiOiJZcE5xSHpEU2dyWWtZeE9vSlpyeDRFM0w1cVBLMUZ4eSJ9.YCsVlQqqlMI_avKGR4Npg32sTqo-FclJ4JLd8AkXPCtHnZrYfe-zlse9QBQmAtN0tJmfye0reeRe2p69ecNXgDo7aWvQHfDRwvc5ys3rAKTMPB5lmLp0WfZD90-yS2s_6bLkfOQ4Hq_v1KeCBCAMlOuBuy4bYXfOSXdcPGgf7AYe9dMP4TpZithtfJeoR8YJv4ssj6lZVCfAZGl0Am4MDD2ZmJmUrzXh1e3hJhykA0PoWEKqfzNova2MqfW4V5PnO0YhhsL0KKfsI1z8wGNDWk2vhKN75MU4C7lWJOuQxYpDbbUy302m9WyapE7s11cx-NlkkLifBAVjf_nkl9dsHA"
		req := acl.GetJwtByTokenReq{
			ClusterId:  1379661944639444077,
			Auth0Token: token,
		}
		res, err := client.GetJwtByToken(ctx, &req)
		if err != nil {
			tilog.PlainInfo("error", zap.Error(err))
		}
		tilog.PlainInfo("res", zap.Any("res", res))
	}

	func testGetDBUsers(accountClient account.AccountAPIServiceClient, ctx context.Context) {
		req := account.GetDBUsersReq{
			OrgId:     30060,
			ProjectId: 22344,
		}
		res, err := accountClient.GetDBUsers(ctx, &req)
		if err != nil {
			tilog.PlainInfo("error", zap.Error(err))
		}
		tilog.PlainInfo("res", zap.Any("res", res))
	}

	func testToken(client account.AccountAPIServiceClient, ctx context.Context) {
		token := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Im1oSjk3OVJEQlFhOWU1U1I4ZUxwRCJ9.eyJodHRwczovL3RpZGJjbG91ZC5jb20vbGFzdF9wYXNzd29yZF9yZXNldCI6IjIwMjItMDYtMDdUMDY6Mjc6MzkuNDE0WiIsImh0dHBzOi8vdGlkYmNsb3VkLmNvbS9sb2dpbl9yZWNlaXZlZF9hdCI6MTY2OTg3NzU2NTM2OCwiZW1haWwiOiJ0b25nLnNoZW5AcGluZ2NhcC5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaXNzIjoiaHR0cHM6Ly90aWRiLWRldi51cy5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NjIxMzJiMjQwNTg5NGIwMDY5ODIwOWM3IiwiYXVkIjoiS2l3V2FqTEozcHRvOFNzWjdGMzVOYXFOaHdub1JoMWMiLCJpYXQiOjE2Njk4Nzc1NjUsImV4cCI6MTY2OTkxMzU2NSwiYXRfaGFzaCI6IjNSYk15SEhKcGpNbkpIWFlLSG8xelEiLCJzaWQiOiJyTWM4S3A1allOb0lNSml6ai05dEZlVDQ0RFhWRzY5aSIsIm5vbmNlIjoiSnA2TzU2cDJ4bklfMmxqbnhOWnk1ZVV6eEt4dG9yZmIifQ.KOfyiH75EBYM97xZCIdCfXCB1wAosj2-mJKtRCT3c3XLa3pQaeLDiOygFOwi4fEHLL0mFAsjXXMo9QkeD8sIjg17-bgoCnnnnyqxD51pkzm7X_JZ7iYMWulE8aJAl2LxrAO3426O-FnVcZeofbgokUHo0OKTQMC1M96HK-CfvMZjmO6vln3pC63EZxMbnKV8swIJFQUjFpISNo6yIVKCC-SGoiDEA-Nf2QfZ8FL_7w3bpxepz23tg8LDIBPZNk9x3S5Eo6mjpQKaHGGvTsMH7jC4AExjHQEO4xe06--p9A9cRluL0NHn_1FL6TL8qyv_WPpJR71rvYmXheWESzdFxg"
		req := account.GetUserByTokenRequest{
			Token: token,
		}
		res, err := client.GetUserByToken(ctx, &req)
		if err != nil {
			tilog.PlainInfo("error", zap.Error(err))
		}
		tilog.PlainInfo("res", zap.Any("res", res))
	}

/*
func testJwt02() {

	addr := "127.0.0.1:10000"
	conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err) //nolint
	}
	defer conn.Close() //nolint
	//accountClient := account.NewAccountAPIServiceClient(conn)
	client := acl.NewAccessServiceClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
	defer cancel() //nolint
	testJwt(client, ctx)

}

	func CreateApiKey() {
		resourceIds := []uint64{1470357}
		req := account.CreateApiKeyReq{
			Source:       "dataapi-console",
			Description:  "wlt-test",
			ScopeType:    "DATAAPP",
			ResourceIds:  resourceIds,
			Role:         "api:dataapp:owner",
			Operator:     "letian.wang@pingcap.com",
			RateLimitRpm: 110,
		}
		ctx := context.Background()
		providerClient := getProviderClient(ctx)
		res, err := providerClient.CreateApiKey(ctx, &req)
		if err != nil {
			tilog.PlainInfo("error", zap.Error(err))
		}
		tilog.PlainInfo("res", zap.Any("res", res))
	}

	func UpdateApiKey() {
		req := account.UpdateApiKeyReq{
			ApiKeyId:    2400923,
			Description: "wlt-test222",
			ScopeType:   "DATAAPP",
			ResourceId:  1470357,

			Operator:     "letian.wang@pingcap.com",
			RateLimitRpm: 110,
		}
		ctx := context.Background()
		providerClient := getProviderClient(ctx)
		res, err := providerClient.UpdateApiKey(ctx, &req)
		if err != nil {
			tilog.PlainInfo("error", zap.Error(err))
		}
		tilog.PlainInfo("res", zap.Any("res", res))
	}

	func gateApiKey() {
		req := account.GetApiKeysByResourceReq{

			ScopeType:  "DATAAPP",
			ResourceId: 1470357,
		}
		ctx := context.Background()
		providerClient := getProviderClient(ctx)
		res, err := providerClient.GetApiKeysByResource(ctx, &req)
		if err != nil {
			tilog.PlainInfo("error", zap.Error(err))
		}
		tilog.PlainInfo("res", zap.Any("res", res))
	}

	func GetCoreSvcClient() core.CoreServiceClient {
		addr := "127.0.0.1:8888"

		conn, err := tigrpc.NewClientConn(addr, tigrpc.WithDefault())
		if err != nil {
			panic(fmt.Sprintf("dial core-svc: %s", err))
		}

		coreSvcCli := core.NewCoreServiceClient(conn)
		tilog.Info(context.Background(), "Initializing core service client", zap.String("addr", addr), zap.Any("err", err))

		return coreSvcCli
	}

	func listOrgs() {
		req := account.OrgFilter{
			OrgTags: []string{"msp"},
		}
		ctx := context.Background()
		providerClient := getProviderClient(ctx)
		res, err := providerClient.ListOrgs(ctx, &req)
		if err != nil {
			tilog.PlainInfo("error", zap.Error(err))
		}
		tilog.PlainInfo("res", zap.Any("res", res))
	}

	func listMspCustomers() {
		req := account.ListMspCustomersReq{
			MspIds: []uint64{2130092},
		}
		ctx := context.Background()
		providerClient := getProviderClient(ctx)
		resp, err := providerClient.ListCustomers(ctx, &req)
		if err != nil {
			tilog.PlainInfo("error", zap.Error(err))
		}
		tilog.PlainInfo("res", zap.Any("resp", resp))
	}

	func getOrgById() {
		req := account.GetOrgByIDRequest{
			Id: 2130103,
		}
		ctx := context.Background()
		providerClient := getProviderClient(ctx)
		res, err := providerClient.GetOrgByID(ctx, &req)
		if err != nil {
			tilog.PlainInfo("error", zap.Error(err))
		}
		tilog.PlainInfo("res", zap.Any("res", res))
	}
func updateOrg() {
	req := account.UpdateOrgRequest{
		Id:    1,
		Kind:  account.OrgKind_ORG_KIND_EXTERNAL,
		Alias: "33",
	}
	ctx := context.Background()
	providerClient := getProviderClient(ctx)
	res, err := providerClient.UpdateOrg(ctx, &req)
	if err != nil {
		tilog.PlainInfo("error", zap.Error(err))
	}
	tilog.PlainInfo("res", zap.Any("res", res))
}
*/

func getProviderClient(ctx context.Context) account.AccountAPIServiceClient {

	addr := "127.0.0.1:10000"
	conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err) //nolint
	}

	providerClient := account.NewAccountAPIServiceClient(conn)
	//client := acl.NewAccessServiceClient(conn)

	return providerClient

}

func listUsersByPage() {
	req := account.ListUsersByPageRequest{
		QueryParam: &account.QueryUserParam{
			EmailLike: "pingcap",
		},

		Pager: &account.Pager{Page: 1, PageSize: 5},
	}
	ctx := context.Background()
	providerClient := getProviderClient(ctx)
	res, err := providerClient.ListUsersByPage(ctx, &req)
	if err != nil {
		tilog.PlainInfo("error", zap.Error(err))
	}
	tilog.PlainInfo("res", zap.Any("res", res))
}
