package config

import (
	"encoding/json"
	"github.com/beego/beego/v2/core/logs"
	"github.com/go-resty/resty/v2"
	"strings"

	"github.com/beego/beego/v2/core/config/env"
)

type RedisConfig struct {
	Host     string `json:"host"`
	Port     int    `json:"port"`
	Password string `json:"password"`
	DB       int    `json:"db"`
}

type Secret struct {
	Key string `json:"key"`
	IV  string `json:"iv"`
}

func GetGatewayHost() (url string) {
	url = env.Get("gatewayAddr", url)
	if url != "" {
		if !strings.HasSuffix(url, "/") {
			url += "/"
		}
		return
	}
	return "http://127.0.0.1:12309/"
}

func GetPortalHost() (url string) {
	url = env.Get("portalAddr", url)
	if url != "" {
		return
	}
	return "http://127.0.0.1:12400/"
}

func GetSecret() (*Secret, error) {
	response, err := resty.New().R().Get("http://kami_backend:12401/api/aes/encryption/params")
	if err != nil {
		logs.Error("请求失败")
		return nil, err
	}
	result := struct {
		Code int    `json:"code"`
		Data Secret `json:"data"`
	}{}
	logs.Info("当前结果是 %v", response.String())
	if err = json.Unmarshal(response.Body(), &result); err != nil {
		return nil, err
	}
	logs.Info("最终结果是：%v", result)
	return &result.Data, nil
}
