package e2e

import (
	"fmt"
	"strings"
)

func ExecCentralSessionCase() {
	Central_LoginConfirm()
	Central_LogoutAllDevices()
}

func Central_LoginConfirm() {
	url := DevConsoleHost + "/login_confirm"

	var header = map[string]string{}
	header["Content-type"] = "application/json"
	header["Origin"] = "https://dev.tidbcloud.com"

	split := strings.Split(consoleAuthorization, " ")
	body := fmt.Sprintf(`{
			"id_token": "%s"
		}`, split[1])

	code, data, err := RestyExec("POST", url, header, body)
	if err != nil {
		code = 0
		data = err.Error()
	}

	SaveCase("Central_Session", "LoginConfirm", CaseDetail{
		Url:       url,
		Method:    "POST",
		ReqHeader: header,
		ReqBody:   body,
		Pass:      code == 200,
		RspCode:   code,
		RspData:   data,
	})
}

func Central_LogoutAllDevices() {
	url := DevConsoleHost + "/api/v1/user/revoke-login-state"

	var header = map[string]string{}
	header["Content-type"] = "application/json"
	header["Authorization"] = consoleAuthorization
	header["Origin"] = "https://dev.tidbcloud.com"

	code, data, err := RestyExec("POST", url, header, "")
	if err != nil {
		code = 0
		data = err.Error()
	}

	SaveCase("Central_Session", "LogoutAllDevices", CaseDetail{
		Url:       url,
		Method:    "POST",
		ReqHeader: header,
		ReqBody:   "",
		Pass:      code == 200,
		RspCode:   code,
		RspData:   data,
	})
}
