package main

import (
	_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
	_ "github.com/gogf/gf/contrib/nosql/redis/v2"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/os/genv"
	"github.com/gogf/gf/v2/os/glog"
	"github.com/gogf/gf/v2/os/gtime"

	_ "kami/internal/boot"
	"kami/internal/cmd"
	_ "kami/internal/logic"
	_ "kami/internal/packed"
	"kami/internal/service"
	"kami/utility/otel"
)

var (
	serviceName = "后台服务——"
)

func main() {
	// 从环境变量获取OTEL服务名称，如果不存在则使用默认值
	otelServiceName := genv.Get("OTEL_SERVICE_NAME")
	if !otelServiceName.IsNil() {
		serviceName += otelServiceName.String()
	}

	// 从环境变量获取OTEL收集器地址，如果不存在则使用默认值
	collectorURL := genv.Get("OTEL_EXPORTER_ENDPOINT").String()
	if collectorURL == "" {
		collectorURL = "38.38.251.113:31547"
	}

	// 初始化OpenTelemetry系统（简化版，与GoFrame集成）
	config := otel.DefaultConfig()
	config.ServiceName = serviceName
	config.CollectorURL = collectorURL
	config.Insecure = true // 根据实际环境调整
	if err := otel.InitWithConfig(config); err != nil {
		glog.Fatal(gctx.GetInitCtx(), "OTel初始化失败", err)
	}

	// 初始化 AES 加密密钥和初始化向量
	if err := service.SysConfigDict().InitAESKeyAndIV(gctx.GetInitCtx()); err != nil {
		glog.Warning(gctx.GetInitCtx(), "AES 密钥初始化失败", err)
	}

	_ = service.ProxyPool().InitStrategy(gctx.GetInitCtx(), "http://api.dmdaili.com/dmgetip.asp?apikey=42bab0ac&pwd=1b567c2f286a08e391b5805565fa0882&getnum=1&httptype=1&geshi=1&fenge=3&fengefu=&operate=all", gtime.M)

	// 测试日志输出
	glog.Info(gctx.GetInitCtx(), "OTel可观测性系统初始化完成，服务启动")
	glog.Debug(gctx.GetInitCtx(), "Debug级别日志测试")
	glog.Warning(gctx.GetInitCtx(), "Warning级别日志测试")

	// 运行主服务
	cmd.Main.Run(gctx.GetInitCtx())
}
