package middleware

import (
	"github.com/ez-api/foundation/requestid"
	"github.com/gin-gonic/gin"
)

// RequestID ensures every request has an X-Request-ID and echoes it back to the client.
func RequestID() gin.HandlerFunc {
	return func(c *gin.Context) {
		id := requestid.Extract(c.GetHeader)
		if id == "" {
			id = requestid.New()
		}
		c.Request.Header.Set(requestid.HeaderName, id)
		c.Writer.Header().Set(requestid.HeaderName, id)
		c.Set("request_id", id)
		c.Next()
	}
}
