// Package classification JNX Robot Control Manager API Documentation.
//
// Schemes: https
// Host: jannex
// BasePath: /v1
// Version: 1.0.0
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// swagger:meta
package main

import (
	"fmt"
	"jannex/robot-control-manager/modules/config"
	"jannex/robot-control-manager/modules/database"
	"jannex/robot-control-manager/modules/robot"
	"jannex/robot-control-manager/modules/utils"
	"jannex/robot-control-manager/routers/router"
	"os"
	"time"

	"git.ex.umbach.dev/Alex/roese-utils/rsconfig"
	"git.ex.umbach.dev/Alex/roese-utils/rslogger"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/cors"
	"github.com/gofiber/fiber/v2/middleware/logger"

	rcmlogger "jannex/robot-control-manager/modules/logger"
)

func init() {
	rsconfig.CreateEnvConfigFileIfNotExists(`DEBUG=false
COLORIZED_OUTPUT=true
HOST=127.0.0.1
PORT=8080

# MariaDB
MARIADB_HOSTNAME=127.0.0.1
MARIADB_PORT=3306
MARIADB_USERNAME=db_user
MARIADB_PASSWORD=db_password
MARIADB_DATABASE_NAME=db_database_name`)

	config.LoadConfig()
	rslogger.InitLogger(true, config.Cfg.ColorizedOutput, config.Cfg.LogManagerServerUrl)

	if os.Getenv("DOCKER") != "" {
		fmt.Println("Waiting for mariadb docker")
		time.Sleep(10 * time.Second)
	}

	database.InitDatabase()
	utils.ValidatorInit()
}

func main() {
	app := fiber.New()

	app.Use(cors.New())

	if config.Cfg.Debug {
		app.Use(logger.New(logger.Config{
			Format: "${pid} ${locals:requestid} ${status} - ${latency} ${method} ${path}​\n",
		}))
	}

	router.SetupRoutes(app)

	rcmlogger.AddSystemLog("Server started")

	robot.LoadRobotsFromDatabase()
	go robot.RobotPingHandler()

	app.Listen(config.Cfg.Host + ":" + config.Cfg.Port)
}
