package main

import (
	"flag"
	"fmt"
	knacos "github.com/go-kratos/kratos/contrib/config/nacos/v2"
	"github.com/go-kratos/kratos/v2"
	"github.com/go-kratos/kratos/v2/config"
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/transport/http"
	"github.com/gogf/gf/util/gconv"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	_ "go.uber.org/automaxprocs"
	"gopkg.in/yaml.v2"
	"os"
	"voucher/internal/conf"
	log2 "voucher/internal/pkg/log"
	"voucher/internal/pkg/mapstructure"
	"voucher/internal/pkg/nacos"
	"voucher/internal/server"
)

// go build -ldflags "-X main.Version=x.y.z"
var (
	// Name is the name of the compiled software.
	Name = "voucher.api"
	// Version is the version of the compiled software.
	Version string

	id, _ = os.Hostname()
)

var (
	nacosIp       = ""
	nacosPort     = 0
	nacosSpace    = ""
	nacosUsername = ""
	nacosPassword = ""
)

const (
	GROUP   = "VOUCHER"
	DATA_ID = "VOUCHER"
)

func init() {
	flag.StringVar(&nacosIp, "nacosIp", "120.55.12.245", "nacos ip address")
	flag.IntVar(&nacosPort, "nacosPort", 8848, "nacos port")
	flag.StringVar(&nacosSpace, "nacosSpace", "voucher-test", "nacos space")
	flag.StringVar(&nacosUsername, "nacosUsername", "nacos", "nacos passowrd")
	flag.StringVar(&nacosPassword, "nacosPassword", "LsxdNacos@2025", "nacos passowrd")
	flag.Parse()
	fmt.Println(nacosIp, nacosPort)
}

func newApp(
	logger log.Logger,
	httpServer *http.Server,
	consumerServer *server.Consumer,
	cronServer *server.CronServer,
	wechatNotifyConsumer *server.WechatNotifyConsumer,
	rdbConsumer *server.RdbConsumer,
) *kratos.App {
	return kratos.New(
		kratos.ID(id),
		kratos.Name(Name),
		kratos.Version(Version),
		kratos.Logger(logger),
		kratos.Server(
			httpServer,
			consumerServer,
			cronServer,
			wechatNotifyConsumer,
			rdbConsumer,
		),
	)
}

func loadConfig() *conf.Bootstrap {
	fmt.Printf(
		"nacosIp:%s,nacosPort:%d,nacosSpace:%s,nacosUsername:%s,nacosPassword:%s,GROUP:%s,DATA_ID:%s",
		nacosIp,
		nacosPort,
		nacosSpace,
		nacosUsername,
		nacosPassword,
		GROUP,
		DATA_ID,
	)
	iConfigClient := nacos.NewNacosConfig(
		nacosIp,
		uint64(nacosPort),
		constant.WithNamespaceId(nacosSpace),
		constant.WithUsername(nacosUsername),
		constant.WithPassword(nacosPassword),
	)
	source := []config.Source{knacos.NewConfigSource(
		iConfigClient,
		knacos.WithGroup(GROUP),
		knacos.WithDataID(DATA_ID),
	)}

	bc := &conf.Bootstrap{}
	for _, c := range source {
		if configs, err := c.Load(); err != nil {
			panic(err)
		} else {
			for _, v := range configs {
				var rs = make(map[string]interface{})
				err = yaml.Unmarshal(v.Value, &rs)
				err = mapstructure.Decode(rs, &bc)
			}
		}
	}

	fmt.Println("远程nacos中的yaml文件的配置:>>>>>> ", gconv.String(&bc))

	return bc
}

func main() {
	bc := loadConfig()
	if bc == nil {
		panic("配置文件加载失败")
	}

	businessLogger := log2.NewBusinessLogger(bc.Logs.Business, Name, Name, Version)
	accessLogger := log2.NewAccessLogger(bc.Logs.Access, id, Name, Version)

	app, cleanup, err := wireApp(bc, businessLogger, accessLogger)
	if err != nil {
		panic(err)
	}
	defer cleanup()

	// start and wait for stop signal
	if err = app.Run(); err != nil {
		panic(err)
	}
}
