package main

import (
	"context"
	"fmt"
	"net/http"
	"strconv"
	"time"

	"github.com/tidbcloud/tiinf/transport/crossregion"
)

func RequestGlobal() {
	ctx := context.TODO()
	envType := crossregion.Dev
	authKey := "CROSS_REGION_AUTH_KEY"
	timeoutSec := 10 * time.Second

	client := crossregion.NewBasicAuthClient(envType, authKey, timeoutSec)
	testUrl := "/inner/eda/api/v1beta/app/data_app_id/endpoint/generate_200"
	respStr, httpCode, err := client.RequestGlobal(ctx, http.MethodPut, testUrl, nil, nil, nil)
	fmt.Println("Request Global - Request Url: " + testUrl)
	if err != nil {
		fmt.Println("Request Global - Invoke failed, error_msg: " + err.Error())
	} else {
		fmt.Println("Request Global - Response Code: " + strconv.Itoa(httpCode))
		fmt.Println("Request Global - Response content: " + respStr)
	}
}

func PostGlobal() {
	ctx := context.TODO()
	envType := crossregion.Dev
	authKey := "CROSS_REGION_AUTH_KEY"
	timeoutSec := 10 * time.Second

	client := crossregion.NewBasicAuthClient(envType, authKey, timeoutSec)
	testUrl := "/status/200"
	respStr, httpCode, err := client.PostGlobal(ctx, testUrl, nil)
	fmt.Println("Request Global - Request Url: " + testUrl)
	if err != nil {
		fmt.Println("Request Global - Invoke failed, error_msg: " + err.Error())
	} else {
		fmt.Println("Request Global - Response Code: " + strconv.Itoa(httpCode))
		fmt.Println("Request Global - Response content: " + respStr)
	}
}

func PostO11yRegion() {
	ctx := context.TODO()
	envType := crossregion.Dev
	authKey := "CROSS_REGION_AUTH_KEY"
	timeoutSec := 10 * time.Second
	region := "us-west-2"

	client := crossregion.NewBasicAuthClient(envType, authKey, timeoutSec)
	testUrl := "/status/200"
	respStr, httpCode, err := client.PostO11yRegion(ctx, region, testUrl, nil)
	fmt.Println("Request O11yRegion - Request Url: " + testUrl)
	if err != nil {
		fmt.Println("Request O11yRegion - Invoke failed, error_msg: " + err.Error())
	} else {
		fmt.Println("Request O11yRegion - Response Code: " + strconv.Itoa(httpCode))
		fmt.Println("Request O11yRegion - Response content: " + respStr)
	}
}

func GetGlobal() {
	ctx := context.TODO()
	envType := crossregion.Dev
	authKey := "CROSS_REGION_AUTH_KEY"
	timeoutSec := 10 * time.Second

	client := crossregion.NewBasicAuthClient(envType, authKey, timeoutSec)
	testUrl := "/status/200"
	respStr, httpCode, err := client.GetGlobal(ctx, testUrl, nil)
	fmt.Println("Request Global - Request Url: " + testUrl)
	if err != nil {
		fmt.Println("Request Global - Invoke failed, error_msg: " + err.Error())
	} else {
		fmt.Println("Request Global - Response Code: " + strconv.Itoa(httpCode))
		fmt.Println("Request Global - Response content: " + respStr)
	}
}

func GetO11yRegion() {
	ctx := context.TODO()
	envType := crossregion.Dev
	authKey := "CROSS_REGION_AUTH_KEY"
	timeoutSec := 10 * time.Second
	region := "us-west-2"

	client := crossregion.NewBasicAuthClient(envType, authKey, timeoutSec)
	testUrl := "/status/200"
	respStr, httpCode, err := client.GetO11yRegion(ctx, region, testUrl, nil)
	fmt.Println("Request O11yRegion - Request Url: " + testUrl)
	if err != nil {
		fmt.Println("Request O11yRegion - Invoke failed, error_msg: " + err.Error())
	} else {
		fmt.Println("Request O11yRegion - Response Code: " + strconv.Itoa(httpCode))
		fmt.Println("Request O11yRegion - Response content: " + respStr)
	}
}

func main() {
	PostGlobal()
	PostO11yRegion()
	GetGlobal()
	GetO11yRegion()
	RequestGlobal()
}
