package env

import (
	"errors"
	"flag"
	"log/slog"
)

var Mode = "dev"
var Name = "dev-edge"

var EndpointAssign = "https://api.lanhuip.com/api/edge/assign"

func Init() error {

	var env = flag.String("e", "dev", "环境变量，可选值 dev 或 prod")
	var name = flag.String("n", "", "节点唯一标识")
	var assign = flag.String("assign", "", "服务发现地址")

	flag.Parse()

	if env != nil && *env != "" {
		if *env == "dev" || *env == "prod" {
			Mode = *env
		} else {
			return errors.New("环境变量只能为 dev 或 prod")
		}
	}

	if name != nil && *name != "" {
		Name = *name
	} else {
		return errors.New("节点唯一标识不能为空")
	}

	if assign != nil && *assign != "" {
		EndpointAssign = *assign
	}

	if Mode == "dev" {
		slog.SetLogLoggerLevel(slog.LevelDebug)
	} else {
		slog.SetLogLoggerLevel(slog.LevelInfo)
	}

	return nil
}
