package middlewares

import (
	"time"

	"doormile/utils"

	"github.com/gofiber/fiber/v2"
)

func ZapLogger() fiber.Handler {
	return func(c *fiber.Ctx) error {
		start := time.Now()

		err := c.Next()

		latency := time.Since(start)
		status := c.Response().StatusCode()
		method := c.Method()
		path := c.Path()

		if err != nil {
			utils.Error("API request error",
				"method", method,
				"path", path,
				"status", status,
				"latency", latency,
				"error", err.Error(),
			)
		} else {
			utils.Info("API request success",
				"method", method,
				"path", path,
				"status", status,
				"latency", latency,
			)
		}

		return err
	}
}
