package api

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/cors"
	"github.com/gofiber/fiber/v2/middleware/logger"
	"github.com/gofiber/fiber/v2/middleware/recover"

	"github.com/sentinela-go/internal/api/middleware"
	"github.com/sentinela-go/internal/config"
	"github.com/sentinela-go/internal/db"
)

func NewServer(cfg *config.Config, database *db.DB) *fiber.App {
	app := fiber.New(fiber.Config{
		AppName: "Sentinela API",
	})

	app.Use(recover.New())
	app.Use(logger.New())
	app.Use(cors.New())
	RegisterSwagger(app)

	app.Use(middleware.NewRateLimiter(cfg.RateLimit))

	if cfg.APIKey != "" {
		app.Use(middleware.NewAPIKeyAuth(cfg.APIKey))
	}

	RegisterRoutes(app, database)

	return app
}
