package e2e

import (
	"encoding/json"
	"fmt"
)

func ExecInnerApiJwtCase() {
	InnerApi_GetJwtByToken()
	InnerApi_GetJwtByApiKey()
	InnerApi_GetJwtByBuiltinUser()
	InnerApi_GetCloudAdminPasswd()

}

func InnerApi_GetJwtByToken() {
	url := DevInnerApiHost + fmt.Sprintf("/inner/account-central/api/v1/user-jwt?user_id=71002&cluster_id=%d", clusterId)
	var header = map[string]string{}
	header["Content-type"] = "application/json"
	header["Authorization"] = innerAuthorization
	code, data, err := RestyExec("GET", url, header, "")
	if err != nil {
		code = 0
		data = err.Error()
	}
	pass := false
	if code == 200 {
		var jwt = &JwtResp{}
		err := json.Unmarshal([]byte(data), jwt)
		if err == nil && jwt.Jwt != "" {
			pass = true
		}
	}
	SaveCase("InnerApi_JwtToken", "GetJwtByToken", CaseDetail{
		Url:       url,
		Method:    "GET",
		ReqHeader: header,
		ReqBody:   "",
		Pass:      pass,
		RspCode:   code,
		RspData:   data,
	})

}

type JwtResp struct {
	Jwt    string `json:"jwt,omitempty"`
	DbUser string `json:"db_user,omitempty"`
}

func InnerApi_GetJwtByApiKey() {
	url := DevInnerApiHost + fmt.Sprintf("/inner/account-central/api/v1/apikey-jwt?access_key=C2DKHJE0&cluster_id=%d", clusterId)
	var header = map[string]string{}
	header["Content-type"] = "application/json"
	header["Authorization"] = innerAuthorization
	code, data, err := RestyExec("GET", url, header, "")
	if err != nil {
		code = 0
		data = err.Error()
	}
	pass := false
	if code == 200 {
		var jwt = &JwtResp{}
		err := json.Unmarshal([]byte(data), jwt)
		if err == nil && jwt.Jwt != "" {
			pass = true
		}
	}
	SaveCase("InnerApi_JwtToken", "GetJwtByApiKey", CaseDetail{
		Url:       url,
		Method:    "GET",
		ReqHeader: header,
		ReqBody:   "",
		Pass:      pass,
		RspCode:   code,
		RspData:   data,
	})

}

func InnerApi_GetJwtByBuiltinUser() {
	url := DevInnerApiHost + "/inner/account-central/api/v1/builtin-user-jwt"
	var header = map[string]string{}
	header["Content-type"] = "application/json"
	header["Authorization"] = innerAuthorization
	body := fmt.Sprintf(`{
		"users": [
				{
					"username": "cloud_ai",
					"org_id": 1440002,
					"cluster_id": %d
				}
			]
	}`, clusterId)
	code, data, err := RestyExec("POST", url, header, body)
	if err != nil {
		code = 0
		data = err.Error()
	}
	pass := false
	if code == 200 {
		var jwts = &BuiltinUserJwtResp{}
		err := json.Unmarshal([]byte(data), jwts)
		if err == nil && len(jwts.Jwts) == 1 && jwts.Jwts[0].Success {
			pass = true
		}
	}
	SaveCase("InnerApi_JwtToken", "GetJwtByBuiltinUser", CaseDetail{
		Url:       url,
		Method:    "POST",
		ReqHeader: header,
		ReqBody:   body,
		Pass:      pass,
		RspCode:   code,
		RspData:   data,
	})
}

type BuiltinUserJwtResp struct {
	Jwts []*BuiltinUserJwt `json:"jwts,omitempty"`
}
type BuiltinUserJwt struct {
	Username            string `json:"username,omitempty"`
	Jwt                 string `json:"jwt,omitempty"`
	TokenExpiredSeconds uint32 `json:"token_expired_seconds,omitempty"`
	OrgId               string `json:"org_id,omitempty"`
	ClusterId           string `json:"cluster_id,omitempty"`
	Success             bool   `json:"success,omitempty"`
	Message             string `json:"message,omitempty"`
}

func InnerApi_GetCloudAdminPasswd() {
	url := DevInnerApiHost + fmt.Sprintf("/inner/account-central/api/v1/cloudadmin-passwd?cluster_id=%d", clusterId)
	var header = map[string]string{}
	header["Content-type"] = "application/json"
	header["Authorization"] = innerAuthorization
	code, data, err := RestyExec("GET", url, header, "")
	if err != nil {
		code = 0
		data = err.Error()
	}
	pass := false
	if code == 200 {
		var jwt = &CloudAdminPasswdResp{}
		err := json.Unmarshal([]byte(data), jwt)
		if err == nil && jwt.Passwd != "" {
			pass = true
		}
	}
	SaveCase("InnerApi_JwtToken", "GetCloudAdminPasswd", CaseDetail{
		Url:       url,
		Method:    "GET",
		ReqHeader: header,
		ReqBody:   "",
		Pass:      pass,
		RspCode:   code,
		RspData:   data,
	})
}

type CloudAdminPasswdResp struct {
	Passwd string `json:"passwd,omitempty"`
}
