package regional_dataapi_gateway

import (
	"bytes"
	"context"
	"dataservices/commons/config"
	"dataservices/ddb"
	dto3 "dataservices/internal/dto/dataapi_control"
	"dataservices/internal/entity"
	"dataservices/internal/repository_ddb"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

// const controlHttpAddr = "http://0.0.0.0:11000"
const controlHttpAddr = "http://0.0.0.0:10020"

func TestCreatePrivateLink(t *testing.T) {
	projectId := uint64(43563)
	clusterId := uint64(2939290)
	serviceName := "com.amazonaws.vpce.us-west-2.vpce-svc-03973c8d762c4f71d"
	url := fmt.Sprintf("/inner/dataservice/projects/%v/clusters/%v/privatelink/create", projectId, clusterId)
	_, code := postApi(t, controlHttpAddr+url, dto3.CreatePrivateLinkReq{
		ServiceName: serviceName,
	})
	require.Equal(t, 200, code)
}

func TestDeletePrivateLink(t *testing.T) {
	projectId := uint64(43563)
	clusterId := uint64(2939290)
	url := fmt.Sprintf("/inner/dataservice/projects/%v/clusters/%v/privatelink/delete", projectId, clusterId)
	_, code := postApi(t, controlHttpAddr+url, nil)
	require.Equal(t, 200, code)
}

func TestPutUserClusterInfo(t *testing.T) {
	projectId := uint64(43563)
	clusterId := uint64(2939290)

	err := config.InitConfig("/Users/cs/.infra-api/regional_dataapi_control.yaml")
	require.NoError(t, err)
	cfg := config.Global()
	ctx := context.Background()
	err = ddb.Init(ctx, cfg)
	require.NoError(t, err)

	info := &entity.UserClusterInfo{
		ProjectId:       projectId,
		ClusterId:       clusterId,
		IsDedicate:      true,
		EndpointService: "",
		EndpointId:      "",
		PrivateDNS:      "",
		Host:            "0.0.0.0",
		Port:            4000,
	}
	err = repository_ddb.PutUserClusterInfo(ctx, ddb.Client(), info)
	require.NoError(t, err)
	fmt.Printf("%#v\n", info)
}

func TestGettUserClusterInfo(t *testing.T) {
	projectId := uint64(43563)
	clusterId := uint64(2939290)

	err := config.InitConfig("/Users/cs/.infra-api/regional_dataapi_control.yaml")
	require.NoError(t, err)
	cfg := config.Global()
	ctx := context.Background()
	err = ddb.Init(ctx, cfg)
	require.NoError(t, err)

	info, err := repository_ddb.GetUserClusterInfo(ctx, ddb.Client(), projectId, clusterId)
	require.NoError(t, err)
	fmt.Printf("%#v\n", info)
}

func TestUpdateUserClusterInfo(t *testing.T) {
	projectId := uint64(43563)
	clusterId := uint64(2939290)

	err := config.InitConfig("/Users/cs/.infra-api/regional_dataapi_control.yaml")
	require.NoError(t, err)
	cfg := config.Global()
	ctx := context.Background()
	err = ddb.Init(ctx, cfg)
	require.NoError(t, err)

	info, err := repository_ddb.GetUserClusterInfo(ctx, ddb.Client(), projectId, clusterId)
	require.NoError(t, err)

	info.Host = "tidb.gswzlgnmb1v.clusters.dev.tidb-cloud.com"
	info.IsDedicate = true
	err = repository_ddb.PutUserClusterInfo(ctx, ddb.Client(), info)
	require.NoError(t, err)
	fmt.Printf("%#v\n", info)
}

func postApi(t *testing.T, url string, req interface{}) (string, int) {
	header := map[string]string{
		"Content-Type": "application/json",
	}
	resp, code, err := PostWithHeader(url, req, &header)
	require.NoError(t, err)
	fmt.Printf("%v %v --------\n\n", resp, code)
	return resp, code
}

var httpClient = &http.Client{Timeout: time.Second * 60}

func PostWithHeader(url string, data interface{}, header *map[string]string) (string, int, error) {
	req, err := http.NewRequest(http.MethodPost, url, encodeJSON(data))
	if err != nil {
		return "", 0, err
	}
	if header != nil {
		for k, v := range *header {
			req.Header.Set(k, v)
		}
	}
	resp, err := httpClient.Do(req)
	if nil != err {
		return "", 0, err
	}
	respdata, err := ioutil.ReadAll(resp.Body)
	return string(respdata), resp.StatusCode, err
}

func encodeJSON(data interface{}) io.Reader {
	b, _ := json.Marshal(data)
	return bytes.NewReader(b)
}
