package server

import (
	"strings"

	"github.com/gofiber/fiber/v2"

	"git.pbiernat.dev/egommerce/go-api-pkg/fluentd"
)

// "github.com/gofiber/fiber/v2"
// "github.com/gofiber/fiber/v2/middleware/cors"

func SetupMiddleware(s *Server) {
	s.Base.Use(defaultCORS)
	s.Base.Use(LoggingMiddleware(s.Logger))
}

func LoggingMiddleware(log *fluentd.Logger) func(c *fiber.Ctx) error {
	return func(c *fiber.Ctx) error {
		path := string(c.Request().URI().Path())
		if strings.Contains(path, "/health") {
			return c.Next()
		}

		log.Log("Request: %s, remote: %s, via: %s",
			c.Request().URI().String(),
			c.Context().RemoteIP().String(),
			string(c.Context().UserAgent()))

		return c.Next()
	}
}
