// Package classification JNX Telegram Bot 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/telegram-bot-manager/modules/config"
	"jannex/telegram-bot-manager/modules/database"
	"jannex/telegram-bot-manager/modules/telegram"
	"jannex/telegram-bot-manager/modules/utils"
	"jannex/telegram-bot-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"

	tblogger "jannex/telegram-bot-manager/modules/logger"
)

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

LOG_MANAGER_SERVER_URL=http://localhost:50110

TELEGRAM_DEBUG=false
TELEGRAM_TOKEN=your_token

# 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(false, 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(fiber.Config{
		// deactivate zero allocation
		Immutable: true,
	})

	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)

	telegram.InitBot()

	tblogger.AddSystemLog(rslogger.LogTypeInfo, "Server started")

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