package script

import (
	"bytes"
	"fmt"
	"github.com/tidbcloud/qa-gourd/ProductModel"
	"github.com/tidbcloud/qa-gourd/ProductService"
	"github.com/tidbcloud/qa-gourd/handler"
	"net/http"
	"testing"
)

const alertURL = "https://open.feishu.cn/open-apis/bot/v2/hook/455e8fc6-09e3-48d5-97df-7cbe10ceff05"

func TestFindNoUseLB(t *testing.T) {
	handler.APIAutoInitialization()
	w := ProductService.GetWebAPIRequestInfo(&testing.T{}, ProductModel.OnDemand)

	lbList := w.FindNoUseELB()
	if len(lbList) == 0 {
		return
	}
	msg := ""
	for _, lb := range lbList {
		temp := fmt.Sprintf(`,[{"tag": "text","text": "%s"}]`, lb)
		msg = msg + temp
	}
	content := fmt.Sprintf(`{"msg_type": "post","content": {"post": {"zh_cn": {"title": "Staging LB Leak","content": [[{"tag": "text","text": "AWS(676546228356) LB 有泄漏: "}]%s]}}}}`, msg)
	jsonStr := []byte(content)

	req, err := http.NewRequest("POST", alertURL, bytes.NewBuffer(jsonStr))
	if err != nil {
		fmt.Println("Error creating request:", err)
		return
	}
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Error sending request:", err)
		return
	}
	defer resp.Body.Close()

	fmt.Println("Response status:", resp.Status)
}
