package middleware

import (
	"context"
	"system-altrak/pkg/utils"

	"github.com/gofiber/fiber/v2"
)

func AuditContextMiddleware() fiber.Handler {
	return func(c *fiber.Ctx) error {
		// Get authenticated user ID from context locals (set by JWTMiddleware)
		userID, _ := c.Locals("user_id").(uint)

		// Create a context carrying the audit_user_id
		ctx := context.WithValue(context.Background(), "audit_user_id", userID)

		// Store in goroutine local storage
		utils.SetContext(ctx)
		defer utils.ClearContext()

		return c.Next()
	}
}
