// internal/middleware/logger.go
package middleware

import (
	"strconv"
	"time"

	"go-server/pkg/logger"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// Logger provides lightweight structured logging and response time header
func Logger() gin.HandlerFunc {
	return func(c *gin.Context) {
		start := time.Now()

		c.Next()

		latency := time.Since(start)
		status := c.Writer.Status()
		method := c.Request.Method
		path := c.Request.URL.Path
		ip := c.ClientIP()
		ua := c.Request.UserAgent()

		// Expose response time for clients/benchmarks
		c.Header("X-Response-Time", strconv.FormatInt(latency.Microseconds(), 10)+"us")

		// Structured log (zap is very fast and minimally blocking)
		logger.Info("http_request",
			zap.Int("status", status),
			zap.String("method", method),
			zap.String("path", path),
			zap.Int64("latency_us", latency.Microseconds()),
			zap.String("ip", ip),
			zap.String("user_agent", ua),
		)
	}
}
