// @APIVersion 1.0.0
// @Title NodeMaster API
// @Description API for managing a node, its services, and a cluster of remote nodes.
// @Contact yves.cerezal@irt-saintexupery.com
// @License Apache 2.0
// @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.html
package routers

import (
	"nodemaster/controllers"

	beego "github.com/beego/beego/v2/server/web"
	"github.com/beego/beego/v2/server/web/filter/cors"
)

func init() {
	// The API has no authentication of its own, so CORS is intentionally
	// scoped to explicit origins (never "*") and only enabled in dev mode
	// for now — enabling it for other run modes, with an operator-supplied
	// origin list, is tracked as a follow-up hardening task.
	if beego.BConfig.RunMode == "dev" {
		origins, err := beego.AppConfig.Strings("corsalloworigins")
		if err != nil || len(origins) == 0 {
			origins = []string{"http://localhost:*"}
		}
		beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
			AllowOrigins: origins,
			AllowMethods: []string{"GET", "POST", "PUT", "DELETE"},
			AllowHeaders: []string{"Origin", "Content-Type"},
		}))
	}

	ns := beego.NewNamespace("/v1",
		beego.NSNamespace("/node",
			beego.NSInclude(
				&controllers.NodeController{},
			),
		),
		beego.NSNamespace("/nodes",
			beego.NSInclude(
				&controllers.NodesController{},
			),
		),
		beego.NSNamespace("/services",
			beego.NSInclude(
				&controllers.ServicesController{},
			),
		),
	)
	beego.AddNamespace(ns)
}
