package lts

import (
	"encoding/json"
	"fmt"
	"strings"
)

const (
	CONTENTTYPE = "application/json"
)

// http 请求
func OpenApiRequest(ak string, sk string, endpoint string, method string, headers map[string]string, querys string, bodys string, path string) string {

	if !strings.HasPrefix(endpoint, "http://") && !strings.HasPrefix(endpoint, "https://") {
		endpoint = "https://" + endpoint
	}
	CT_IAM_URL := fmt.Sprintf("%s%s", endpoint, path)
	fmt.Println("request url:", CT_IAM_URL)
	WriteCommandToLog(CT_IAM_URL)

	ctrequest := CTRequest{
		Method:      method,
		ContentType: CONTENTTYPE,
		Uri:         CT_IAM_URL,
		UriParam:    nil,
		QueryParam:  querys,
		HeaderParam: headers,
		BodyByteArr: nil,
		BodyParam:   bodys,
		Body:        nil,
		FileMap:     nil,
	}
	response := Http(ctrequest.ContentType, ctrequest.Method, ctrequest.Uri, ak, sk, ctrequest.HeaderParam, ctrequest.QueryParam, ctrequest.BodyParam, ctrequest.FileMap, ctrequest.Body)

	WriteCommandToLog(response)
	return response
}

// ********************日志项目**********************

// 创建一个日志项目
func CreateProject(ak string, sk string, endpoint string, projectName string, description *string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	bodymap := map[string]any{
		"projectName": projectName,
	}
	if description != nil {
		bodymap["description"] = *description
	}
	bodysjson, _ := json.Marshal(bodymap)
	bodys := string(bodysjson)
	querys := ""
	path := "/v1/project/create"
	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 删除指定日志项目。当日志项目中的存在日志单元，需要先删除日志单元后才可以删除
func DeleteProject(ak string, sk string, endpoint string, projectCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	bodymap := map[string]any{
		"projectCode": projectCode,
	}
	bodysjson, _ := json.Marshal(bodymap)
	bodys := string(bodysjson)
	querys := ""
	path := "/v1/project/delete"
	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 修改指定日志项目的信息
func UpdateProject(ak string, sk string, endpoint string, projectCode string, description *string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	bodymap := map[string]any{
		"projectCode": projectCode,
	}
	if description != nil {
		bodymap["description"] = *description
	}
	bodysjson, _ := json.Marshal(bodymap)
	bodys := string(bodysjson)

	fmt.Println(bodys)
	querys := ""
	path := "/v1/project/update"
	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 查询所有日志项目
func ListProject(ak string, sk string, endpoint string, projectName *string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""
	querys := ""
	if projectName != nil {
		querys = fmt.Sprintf("projectName=%s", *projectName)
	}
	fmt.Println("querys:", querys)
	path := "/v1/project/list"
	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 根据项目名获取项目编码
func GetCodeByName(ak string, sk string, endpoint string, projectName string, regionId string) string {
	headers := make(map[string]string)
	headers["regionId"] = regionId
	method := "get"
	bodys := ""
	querys := fmt.Sprintf("projectName=%s", projectName)
	path := "/v1/project/getCodeByName"
	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 获取日志项目的数量。
func CountProject(ak string, sk string, endpoint string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""
	querys := ""
	path := "/v1/project/count"

	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 获取项目摘要
func GetProject(ak string, sk string, endpoint string, projectCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""
	querys := fmt.Sprintf("projectCode=%s", projectCode)
	path := "/v1/project/get"
	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 根据项目编码重命名项目。
func RenameProject(ak string, sk string, endpoint string, projectCode string, aliasName string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	querys := ""
	path := "/v1/project/renameProject"

	bodymap := map[string]any{
		"projectCode": projectCode,
		"aliasName":   aliasName,
	}
	bodys, _ := json.Marshal(bodymap)

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, string(bodys), path)

	return responseBody
}

// 根据项目编码更新项目描述。
func UpdateProjectDescription(ak string, sk string, endpoint string, projectCode string, description string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	querys := ""
	path := "/v1/project/updateProjectDesc"

	bodymap := map[string]any{
		"projectCode": projectCode,
		"description": description,
	}
	bodys, err := json.Marshal(bodymap)
	if err != nil {
		fmt.Println("Error marshalling body:", err)
		return ""
	}

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, string(bodys), path)

	return responseBody
}

// 根据项目编码获取项目描述。
func GetProjectDescription(ak string, sk string, endpoint string, projectCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("projectCode=%s", projectCode)

	path := "/v1/project/getProjectDesc"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 获取项目详情
func GetProjectByCode(ak string, sk string, endpoint string, projectCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("projectCode=%s", projectCode)

	path := "/v1/project/getByCode"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 根据项目原始名称和项目编码，检查项目是否存在
func CheckProjectExist(ak string, sk string, endpoint string, projectCode string, projectName string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("projectCode=%s&projectName=%s", projectCode, projectName)

	path := "/v1/project/checkExist"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 根据项目原始名称，查询项目的别名。
func QueryAliasName(ak string, sk string, endpoint string, projectNameList string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("projectNameList=%s", projectNameList)

	path := "/v1/project/queryAliasName"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 根据项目编码，查询项目的原始名称。
func QueryNameByCode(ak string, sk string, endpoint string, projectCodeList string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("projectCodeList=%s", projectCodeList)

	path := "/v1/project/queryNameByCode"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 分页查询当前租户在当前资源池下的日志项目。
func PageProject(ak string, sk string, endpoint string, projectName *string, pageNum int, pageSize int, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	queryParams := []string{
		fmt.Sprintf("pageSize=%d&pageNum=%d", pageSize, pageNum),
	}

	if projectName != nil && len(strings.TrimSpace(*projectName)) > 0 {
		queryParams = append(queryParams, fmt.Sprintf("projectName=%s", *projectName))
	}

	querys := strings.Join(queryParams, "&")

	path := "/v1/project/page"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// ********************日志单元**********************

// 在日志项目内创建一个日志单元
func CreateLogUnit(ak string, sk string, endpoint string, projectCode string, unitName string, ttlDays int, description *string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	bodymap := map[string]any{
		"projectCode": projectCode,
		"unitName":    unitName,
	}
	if description != nil {
		bodymap["description"] = *description
	}
	if ttlDays > 0 {
		bodymap["ttlDays"] = ttlDays
	}
	bodysjson, _ := json.Marshal(bodymap)
	bodys := string(bodysjson)
	querys := ""
	path := "/v1/unit/create"
	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 修改指定日志单元下的日志存储时长和描述
func UpdateLogUnit(ak string, sk string, endpoint string, projectCode string, unitCode string, ttlDays int, description *string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"

	bodymap := map[string]any{
		"projectCode": projectCode,
		"unitCode":    unitCode,
	}

	if ttlDays > 0 {
		bodymap["ttlDays"] = ttlDays
	}

	if description != nil {
		bodymap["description"] = *description
	}

	bodysjson, _ := json.Marshal(bodymap)
	bodys := string(bodysjson)

	querys := "" // 没有Query参数
	path := "/v1/unit/update"

	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 删除指定日志项目下的指定日志单元
func DeleteLogUnit(ak string, sk string, endpoint string, projectCode string, unitCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"

	bodymap := map[string]any{
		"projectCode": projectCode,
		"unitCode":    unitCode,
	}

	bodysjson, _ := json.Marshal(bodymap)
	bodys := string(bodysjson)

	querys := ""
	path := "/v1/unit/delete"

	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 查询指定日志项目下的所有日志单元信息
func ListLogUnit(ak string, sk string, endpoint string, projectCode string, unitName *string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""
	querys := ""
	if unitName != nil {
		querys = fmt.Sprintf("projectCode=%s&unitName=%s", projectCode, *unitName)
	} else {
		querys = fmt.Sprintf("projectCode=%s", projectCode)
	}

	path := "/v1/unit/list"
	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 分页查询指定日志项目下的日志单元信息。
func PageLogUnit(ak string, sk string, endpoint string, projectCode string, unitName *string, pageNum int, pageSize int, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	queryParams := []string{
		fmt.Sprintf("projectCode=%s", projectCode),
	}

	if unitName != nil && len(strings.TrimSpace(*unitName)) > 0 {
		queryParams = append(queryParams, fmt.Sprintf("unitName=%s", *unitName))
	}
	if pageNum > 0 {
		queryParams = append(queryParams, fmt.Sprintf("pageNum=%d", pageNum))
	}
	if pageSize > 0 {
		queryParams = append(queryParams, fmt.Sprintf("pageSize=%d", pageSize))
	}

	querys := strings.Join(queryParams, "&")

	path := "/v1/unit/page"
	fmt.Println("querys:", querys)
	return OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
}

// 查询指定日志单元的索引信息。
func GetIndex(ak string, sk string, endpoint string, projectCode string, unitCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""
	querys := fmt.Sprintf("projectCode=%s&unitCode=%s", projectCode, unitCode)
	path := "/v1/index/getIndex"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 更新指定日志单元的索引信息，更新后用此索引配置覆盖原有配置。
func UpdateIndex(ak string, sk string, endpoint string, projectCode string, unitCode string, fullTextIndex bool, fieldName []string, dataType []string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	bodymap := map[string]any{
		"projectCode":   projectCode,
		"unitCode":      unitCode,
		"fullTextIndex": fullTextIndex,
	}
	fields := make([]map[string]string, len(fieldName))
	for i := 0; i < len(fieldName); i++ {
		field := map[string]string{
			"fieldName": fieldName[i],
			"dataType":  dataType[i],
		}
		fields[i] = field
	}
	bodymap["fields"] = fields
	bodysjson, _ := json.Marshal(bodymap)
	bodys := string(bodysjson)

	querys := ""
	path := "/v1/index/updateIndex"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 为指定日志单元创建索引。
func CreateIndex(ak string, sk string, endpoint string, projectCode string, unitCode string, fullTextIndex bool, fieldName []string, dataType []string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	bodymap := map[string]any{
		"projectCode":   projectCode,
		"unitCode":      unitCode,
		"fullTextIndex": fullTextIndex,
	}
	fields := make([]map[string]string, len(fieldName))
	for i := 0; i < len(fieldName); i++ {
		field := map[string]string{
			"fieldName": fieldName[i],
			"dataType":  dataType[i],
		}
		fields[i] = field
	}
	bodymap["fields"] = fields
	bodysjson, _ := json.Marshal(bodymap)
	bodys := string(bodysjson)

	querys := ""
	path := "/v1/index/createIndex"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 删除指定日志单元的索引（包括全文索引）。
func DeleteIndex(ak string, sk string, endpoint string, projectCode string, unitCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("projectCode=%s&unitCode=%s", projectCode, unitCode)

	path := "/v1/index/deleteIndex"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 更新日志单元TTL。
func UpdateTtl(ak string, sk string, endpoint string, unitCode string, ttlDays int, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	querys := ""
	path := "/v1/unit/updateTtl"

	bodyMap := map[string]any{
		"unitCode": unitCode,
	}
	if ttlDays > 0 {
		bodyMap["ttlDays"] = ttlDays
	}
	fmt.Println("bodyMap:", bodyMap)
	bodys, _ := json.Marshal(bodyMap)

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, string(bodys), path)

	return responseBody
}

// 查询指定日志项目的单元数量。
func CountUnit(ak string, sk string, endpoint string, projectCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""
	querys := fmt.Sprintf("projectCode=%s", projectCode)

	path := "/v1/unit/count"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 通过日志单元ID获取日志单元。
func GetUnitByCode(ak string, sk string, endpoint string, unitCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("unitCode=%s", unitCode)

	path := "/v1/unit/get"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 获取单元索引数量。
func GetIndexCount(ak string, sk string, endpoint string, unitCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("unitCode=%s", unitCode)

	path := "/v1/index/getIndexCount"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 根据日志单元ID更新日志单元描述。
func UpdateLogUnitDescription(ak string, sk string, endpoint string, unitCode string, description string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	querys := ""
	path := "/v1/unit/updateLogUnitNote"

	bodymap := map[string]any{
		"unitCode":    unitCode,
		"description": description,
	}
	bodys, err := json.Marshal(bodymap)
	if err != nil {
		fmt.Println("Error marshalling body:", err)
		return ""
	}

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, string(bodys), path)

	return responseBody
}

// 查询指定日志单元的标签列表。
func GetLogUnitTags(ak string, sk string, endpoint string, unitCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("unitCode=%s", unitCode)

	path := "/v1/unit/getLogUnitTags"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 查询指定日志单元的备注。
func GetLogUnitNote(ak string, sk string, endpoint string, unitCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("unitCode=%s", unitCode)

	path := "/v1/unit/getLogUnitNote"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 根据日志单元ID查询日志单元原始名称。
func GetLogUnitOriginName(ak string, sk string, endpoint string, unitCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("unitCode=%s", unitCode)

	path := "/v1/unit/getLogUnitOriginName"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 根据日志单元ID查询日志单元别名。
func GetLogUnitAliasName(ak string, sk string, endpoint string, unitCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("unitCode=%s", unitCode)

	path := "/v1/unit/getLogUnitAliasName"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 根据日志单元ID重命名日志单元别名。
func RenameLogUnit(ak string, sk string, endpoint string, unitCode string, aliasName string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	querys := ""
	path := "/v1/unit/renameLogUnit"

	bodymap := map[string]any{
		"unitCode":  unitCode,
		"aliasName": aliasName,
	}
	bodys, err := json.Marshal(bodymap)
	if err != nil {
		fmt.Println("Error marshalling body:", err)
		return ""
	}

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, string(bodys), path)

	return responseBody
}

// 查询指定日志项目下日志单元别名列表。
func QueryLogUnitAliasNames(ak string, sk string, endpoint string, projectCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("projectCode=%s", projectCode)

	path := "/v1/unit/queryLogUnitAliasNames"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 查询指定日志项目下日志单元原始名称列表。
func QueryLogUnitOriginNames(ak string, sk string, endpoint string, projectCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("projectCode=%s", projectCode)

	path := "/v1/unit/queryLogUnitOriginNames"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 查询日志单元日志存储时长。
func GetLogUnitTtlDays(ak string, sk string, endpoint string, unitCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("unitCode=%s", unitCode)

	path := "/v1/unit/getLogUnitTtlDays"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 检查日志单元是否存在。
func CheckLogUnitExist(ak string, sk string, endpoint string, unitName string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""

	querys := fmt.Sprintf("unitName=%s", unitName)

	path := "/v1/unit/checkLogUnitExist"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// ********************日志检索**********************
func GetLogs(ak string, sk string, endpoint string, projectCode string, unitCode string, startTime int64, endTime int64, query string, reverse bool, searchType string, limit int, cursor string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	querys := ""

	bodymap := map[string]any{
		"projectCode": projectCode,
		"unitCode":    unitCode,
		"startTime":   startTime,
		"endTime":     endTime,
		"query":       query,
		"reverse":     reverse,
		"searchType":  searchType,
		"limit":       limit,
		"cursor":      cursor,
	}

	bodysjson, _ := json.Marshal(bodymap)
	bodys := string(bodysjson)

	path := "/v1/content/getLogs"

	return OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
}

// *******************服务开通**********************

// 开通云日志服务。
func OrderLogInstance(ak string, sk string, endpoint string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	querys := ""
	path := "/v1/order/instance/open"

	bodys := ""

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 检查订单实例状态。
func CheckLicense(ak string, sk string, endpoint string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""
	querys := ""

	path := "/v1/order/instance/checkLicense"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 获取租户资源池下实例信息。
func GetInstanceInfo(ak string, sk string, endpoint string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""
	querys := ""

	path := "/v1/usage/instance/getInstanceInfo"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// 获取实例的开通状态。
func GetInstanceStatus(ak string, sk string, endpoint string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	bodys := ""
	querys := ""

	path := "/v1/usage/instance/getInstanceStatus"

	responseBody := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)

	return responseBody
}

// *******************日志下载*******************
// 获取指定下载任务的下载链接。
func GetSignUrlDownload(ak string, sk string, endpoint string, taskCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	// querys := ""
	querys := fmt.Sprintf("taskCode=%s", taskCode)
	path := "/v1/dataDownload/getSignUrl"
	// bodymap := map[string]any{
	// 	"taskCode": taskCode,
	// }
	// bodysJSON, _ := json.Marshal(bodymap)
	// bodys := string(bodysJSON)
	bodys := ""

	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 获取指定下载任务信息。
func QueryDownload(ak string, sk string, endpoint string, taskCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	// querys := ""
	querys := fmt.Sprintf("taskCode=%s", taskCode)
	path := "/v1/dataDownload/query"
	// bodymap := map[string]any{
	// 	"taskCode": taskCode,
	// }
	// bodysJSON, _ := json.Marshal(bodymap)
	// bodys := string(bodysJSON)
	bodys := ""

	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 获取指定单元下载任务列表。
func ListTaskDownload(ak string, sk string, endpoint string, unitCode string, pageSize int, pageNum int, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "get"
	// querys := ""
	querys := fmt.Sprintf("unitCode=%s", unitCode)
	path := "/v1/dataDownload/listTask"
	bodyMap := map[string]any{
		// "unitCode": unitCode,
	}
	if pageSize > 0 {
		bodyMap["pageSize"] = pageSize
	}
	if pageNum > 0 {
		bodyMap["pageNum"] = pageNum
	}
	bodysJSON, _ := json.Marshal(bodyMap)
	bodys := string(bodysJSON)

	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 删除下载任务。

func DeleteDownload(ak string, sk string, endpoint string, taskCode string, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	querys := ""
	path := "/v1/dataDownload/delete"
	bodymap := map[string]any{
		"taskCode": taskCode,
	}
	bodysJSON, _ := json.Marshal(bodymap)
	bodys := string(bodysJSON)

	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}

// 创建下载任务
func CreateDownload(ak string, sk string, endpoint string, unitCode string, from int64, to int64, customLogNum int, query string, format string, reverse bool, regionId string) string {
	headers := map[string]string{
		"regionId": regionId,
	}
	method := "post"
	querys := ""
	path := "/v1/dataDownload/create"
	bodyMap := map[string]any{
		"unitCode":     unitCode,
		"from":         from,
		"to":           to,
		"customLogNum": customLogNum,
		"query":        query,
		"reverse":      reverse,
		"format":       format,
	}
	bodysJSON, _ := json.Marshal(bodyMap)
	bodys := string(bodysJSON)

	response_body := OpenApiRequest(ak, sk, endpoint, method, headers, querys, bodys, path)
	return response_body
}
