package main

import (
	"fmt"
	_ "oc-workflow/routers"
	"os"

	oclib "cloud.o-forge.io/core/oc-lib"
	beego "github.com/beego/beego/v2/server/web"
	"golang.org/x/mod/modfile"
)

const appname = "oc-workflow"

func main() {
	// Init the oc-lib
	oclib.Init(appname)

	// Load the right config file
	o := oclib.GetConfLoader()

	// feed the library with the loaded config
	oclib.SetConfig(
		o.GetStringDefault("MONGO_URL", "mongodb://127.0.0.1:27017"),
		o.GetStringDefault("MONGO_DATABASE", "DC_myDC"),
		o.GetStringDefault("NATS_URL", "nats://localhost:4222"),
		o.GetStringDefault("lokiurl", ""),
		o.GetStringDefault("loglevel", "info"),
	)

	/* PATHS ARE REFERENCE FOR INNER SERVICE OF DISTANT OC
	 * PATHS ARE USED TO CALL OTHER OC SERVICES
	 * NAMES ARE CANONICAL NAMES OF THE SERVICES
	 */
	oclib.AddPath(oclib.LibDataEnum(oclib.BOOKING), o.GetStringDefault("BOOKING_URL", ":8092"))
	oclib.AddPath(oclib.LibDataEnum(oclib.PEER), o.GetStringDefault("PEER_URL", ":8093"))
	oclib.AddPath(oclib.LibDataEnum(oclib.WORKFLOW), o.GetStringDefault("WORKFLOW_URL", ":"+o.GetStringDefault("PORT", "8088")))

	// Beego init
	beego.BConfig.AppName = appname
	beego.BConfig.Listen.HTTPPort = o.GetIntDefault("port", 8080)
	beego.BConfig.WebConfig.DirectoryIndex = true
	beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"

	beego.Run()
}

func GetModuleName() string {
	goModBytes, err := os.ReadFile("go.mod")
	if err != nil {
		return "prrr"
	}

	modName := modfile.ModulePath(goModBytes)
	fmt.Fprintf(os.Stdout, "modName=%+v\n", modName)

	return modName
}
