package middleware

import (
	"log"
	"time"

	"github.com/gin-gonic/gin"
)

// Logger is a middleware that logs HTTP requests
func Logger(c *gin.Context) {
	start := time.Now()

	// Process request
	c.Next()

	// Log the request details
	duration := time.Since(start)
	statusCode := c.Writer.Status()
	method := c.Request.Method
	path := c.Request.URL.Path

	log.Printf("%s %s %d %s %s", method, path, statusCode, duration, c.ClientIP())
	if statusCode >= 400 {
		log.Printf("Error: %s %s %d", method, path, statusCode)
	}
	// Log the response size
	responseSize := c.Writer.Size()
	log.Printf("Response size: %d bytes", responseSize)
	// Log the request duration
	log.Printf("Request duration: %s", duration)
	// Log the client IP
	clientIP := c.ClientIP()
	log.Printf("Client IP: %s", clientIP)
}
