package config

import (
	"github.com/beego/beego/v2/core/config/env"
	"github.com/beego/beego/v2/server/web"
)

func GetGatewayAddr() string {
	return "http://kami_gateway:12309"
}

func GetShopAddr() string {
	shopAddr := env.Get("shopAddr", "")
	if shopAddr != "" {
		return shopAddr
	}
	return web.AppConfig.DefaultString("shopAddr", web.AppConfig.DefaultString("shopHost", "https://127.0.0.1:12305"))
}

func GetPortalAddr() string {
	portalAddr := env.Get("portalAddr", "")
	if portalAddr != "" {
		return portalAddr
	}
	return web.AppConfig.DefaultString("portalAddr", web.AppConfig.DefaultString("frontendHost", "https://127.0.0.1:22400"))
}

func GetRedisCfg() (RedisCfg, error) {
	host, err := web.AppConfig.String("redis::host")
	if err != nil {
		return RedisCfg{}, err // 返回错误
	}

	port, err := web.AppConfig.Int("redis::port")
	if err != nil {
		return RedisCfg{}, err // 返回错误
	}
	password, err := web.AppConfig.String("redis::password")
	if err != nil {
		return RedisCfg{}, err // 返回错误
	}

	return RedisCfg{
		Host:     host,
		Port:     port,
		Password: password,
	}, nil
}

func GetSecret() *Secret {
	// 获取secret配置
	secret := &Secret{
		Key: web.AppConfig.DefaultString("secret::key", "thisis32bitlongpassphraseimusing"),
		IV:  web.AppConfig.DefaultString("secret::iv", "1234567890123456"),
	}
	// 使用secret...
	return secret
}

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

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