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)

	// Plan-based rate limiting and access control (replaces old rate limiter + API key auth)
	app.Use(middleware.NewPlanMiddleware(database))

	RegisterRoutes(app, database)

	return app
}
