package middleware

import (
	"apskel-pos-be/internal/appcontext"
	"apskel-pos-be/internal/constants"
	"context"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

func CorrelationID() gin.HandlerFunc {
	return func(c *gin.Context) {
		correlationID := c.GetHeader(constants.CorrelationIDHeader)
		if correlationID == "" {
			correlationID = uuid.New().String()
		}
		ctx := context.WithValue(c.Request.Context(), appcontext.CorrelationIDKey, correlationID)
		c.Request = c.Request.WithContext(ctx)
		c.Writer.Header().Set(constants.CorrelationIDHeader, correlationID)
		c.Next()
	}
}
