package main

import (
	"message-service/commons/httpclient"
	"strconv"

	"github.com/pingcap/log"
	"go.uber.org/zap"
)

const ()

type Data struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func main() {

	orgId := 30061
	projectId := 43563
	clusterId := 120790
	//orgId := 60232
	//projectId := 73844
	//clusterId := 120794
	token := "bing.wang@pingcap.com"
	localhost := "http://127.0.0.1:10010"
	// testInteractBot(localhost, token, orgId, projectId)
	testInteractBotSqleditorDatabase(localhost, token, orgId, projectId, clusterId)
	//testInteractBotSqleditorNoDatabase(localhost, token, orgId, projectId, clusterId)
	return
	devToken := "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Im1oSjk3OVJEQlFhOWU1U1I4ZUxwRCJ9.eyJodHRwczovL3RpZGJjbG91ZC5jb20vbG9naW5fcmVjZWl2ZWRfYXQiOjE2NzIzNzExNjc1ODMsImVtYWlsIjoiYmluZy53YW5nQHBpbmdjYXAuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImlzcyI6Imh0dHBzOi8vdGlkYi1kZXYudXMuYXV0aDAuY29tLyIsInN1YiI6Imdvb2dsZS1vYXV0aDJ8MTA0Mjg1ODQ5MzQ4MDgzMDY0NjgwIiwiYXVkIjoiS2l3V2FqTEozcHRvOFNzWjdGMzVOYXFOaHdub1JoMWMiLCJpYXQiOjE2NzIzNzExNjcsImV4cCI6MTY3MjQwNzE2NywiYXRfaGFzaCI6IlBhVFViMXpvamc2VFE5M2lFcmg0WHciLCJzaWQiOiJOaVZMQlBUWGRLeW9Ddk5NeXpOalNDSGZKNmJKMXc2MyIsIm5vbmNlIjoiUkVpdm9Za2FHWk1WeU9fOEdMdjhuTDlEeDdjYkV1MDcifQ.osD5ppBock7Fa8-XsnOY93j0AwZ22OPsPtq65IokcJLKMtDSKfxzYDr7asXX-_ZFcMIcAGthostuv2mfapwJrF6_yiPwsYV0e6ibnDNNDPmIjHrnhn09jzGDnUvmYluIDgbtVU0ue_uOp-S9rNJizl5ahAW2yQBir81V5uk5hIvZhorW9Zx5QENszai9iUlamDJiM06Gtw0KOskxvkQdMzgU7tTxGOFMZSLpFMgDaRp_x1_X1F_zfmcl1CcQ6FIlb4mmzle14ctqV1KXgeNN4bLliyI9LvKikH7_kQGIKESSH9X5i437WrUyNAreiZaRlI5tOETXllliyahh-_UCjg"
	devhost := "https://us-west-2.nightly.shared.aws.tidbcloud.com"
	testInteractBotSqleditorDatabase(devhost, devToken, orgId, projectId, clusterId)
	/*
			orgId = 1372813089202881281
			projectId = 1372813089200961282
			prodToken := ""
			prodhost := "https://us-west-2.prod.aws.tidbcloud.com"
			testInteractBot(prodhost, prodToken, orgId, projectId)

		orgId = 1369847559691367421
		projectId = 1369847559691368078
		stagingToken := ""
		staginghost := "https://us-west-2.staging.shared.aws.tidbcloud.com"
		testInteractBot(staginghost, stagingToken, orgId, projectId)
	*/

}

func testInteractBotSqleditorDatabase(localhost string, token string, orgId int, projectId int, clusterId int) {
	header := map[string]string{"test-email": token, "authorization": token}
	url := localhost + "/api/v1/message/orgs/" + strconv.Itoa(orgId) + "/projects/" + strconv.Itoa(projectId) + "/bot"
	params := map[string]interface{}{
		"question":   "get latest record",
		"bot_type":   "sqleditor_database",
		"database":   "information_schema",
		"cluster_id": strconv.Itoa(clusterId),
	}
	// params := map[string]string{"question": "give me sql to find how many repo were written by python", "bot_type": "githubevents"}
	resp, code, err := httpclient.PostWithHeader(url, params, &header)
	if err != nil {
		log.Error("fail to get response ", zap.Error(err))
	}
	log.Info("resp code is  " + strconv.Itoa(code) + " body is " + resp)
}

func testInteractBotSqleditorNoDatabase(localhost string, token string, orgId int, projectId int, clusterId int) {
	header := map[string]string{"test-email": token, "authorization": token}
	url := localhost + "/api/v1/message/orgs/" + strconv.Itoa(orgId) + "/projects/" + strconv.Itoa(projectId) + "/bot"
	params := map[string]interface{}{
		"question":   "Find the most popular repo from github_events",
		"bot_type":   "sqleditor_no_database",
		"cluster_id": strconv.Itoa(clusterId),
	}
	// params := map[string]string{"question": "give me sql to find how many repo were written by python", "bot_type": "githubevents"}
	resp, code, err := httpclient.PostWithHeader(url, params, &header)
	if err != nil {
		log.Error("fail to get response ", zap.Error(err))
	}
	log.Info("resp code is  " + strconv.Itoa(code) + " body is " + resp)
}

func testInteractBot(localhost string, token string, orgId int, projectId int) {
	header := map[string]string{"test-email": token, "authorization": token}
	url := localhost + "/api/v1/message/orgs/" + strconv.Itoa(orgId) + "/projects/" + strconv.Itoa(projectId) + "/bot"
	params := map[string]string{"question": "坚决拥护共产党", "bot_type": "githubevents"}
	// params := map[string]string{"question": "give me sql to find how many repo were written by python", "bot_type": "githubevents"}
	resp, code, err := httpclient.PostWithHeader(url, params, &header)
	if err != nil {
		log.Error("fail to get response ", zap.Error(err))
	}
	log.Info("resp code is  " + strconv.Itoa(code) + " body is " + resp)
}
