// internal/middleware/logger.go
package middleware

import (
	"fmt"
	"time"

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

// LoggerMiddleware logs HTTP requests
func LoggerMiddleware() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		// Start timer
		startTime := time.Now()

		// Process request
		ctx.Next()

		// Calculate latency
		latency := time.Since(startTime)

		// Get request info
		method := ctx.Request.Method
		path := ctx.Request.URL.Path
		statusCode := ctx.Writer.Status()
		clientIP := ctx.ClientIP()

		// Get user ID if authenticated
		userID := "guest"
		if id, exists := ctx.Get("user_id"); exists {
			userID = fmt.Sprintf("%v", id)
		}

		// Log format
		fmt.Printf("[%s] %s | %3d | %13v | %15s | %s | User: %s\n",
			time.Now().Format("2006-01-02 15:04:05"),
			method,
			statusCode,
			latency,
			clientIP,
			path,
			userID,
		)
	}
}