package main

import (
	"fmt"
	"log"
	"nearle/db"
	"nearle/facade"
	"nearle/models"
	"nearle/routes"
	"os"
	"os/signal"
	"strings"
	"syscall"
	"time"
	_ "time/tzdata"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/cors"
	"github.com/joho/godotenv"
	"gorm.io/gorm"
)

func init() {
	godotenv.Load()
}

func main() {

	app := fiber.New()

	app.Use(cors.New(cors.Config{
		AllowHeaders:     "Origin,Content-Type,Accept,Content-Length,Accept-Language,Accept-Encoding,Connection,Access-Control-Allow-Origin",
		AllowOrigins:     "*",
		AllowCredentials: true,
		AllowMethods:     "GET,POST,HEAD,PUT,DELETE,PATCH,OPTIONS",
	}))

	fmt.Println("🌐 Connecting to databases...")
	db.Connect()
	fmt.Println("✅ Database connections established!")

	// Ensure schema is updated
	db.DB.AutoMigrate(&models.StockRequest{})

	f := facade.NewFacade(db.DB, db.CatalogueDB)

	routes.RegisterRoutes(app, f)

	// Start server
	go func() {
		if err := app.Listen(":1122"); err != nil {
			log.Fatal("Server failed to start:", err)
		}
	}()

	gracefulShutdown()
}

func selectDBMiddleware(c *fiber.Ctx) error {
	path := c.Path()
	result := strings.Split(path, "/")

	var flavour string
	if len(result) > 1 {
		flavour = result[1]
	}

	var currentDB *gorm.DB
	switch flavour {
	case "dev", "live":
		currentDB = db.DB
	}

	if currentDB != nil {
		c.Locals("DB", currentDB)
	}

	return c.Next()
}

func gracefulShutdown() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM)

	<-c
	fmt.Println("\nShutting down gracefully...")

	// Normally: close db.DB_DEV and db.DB_LIVE
	// Example:
	// closeDB(db.DB_DEV)
	// closeDB(db.DB_LIVE)

	time.Sleep(2 * time.Second)
	fmt.Println("Shutdown complete.")

	os.Exit(0)
}
