package middleware

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/perfect-panel/server/pkg/constant"
)

func ApiVersionSwitchHandler(legacyHandler gin.HandlerFunc, latestHandler gin.HandlerFunc) gin.HandlerFunc {
	return func(c *gin.Context) {
		useLatest, _ := c.Request.Context().Value(constant.CtxKeyAPIVersionUseLatest).(bool)
		if useLatest && latestHandler != nil {
			latestHandler(c)
			return
		}
		if legacyHandler != nil {
			legacyHandler(c)
			return
		}
		c.AbortWithStatus(http.StatusNotFound)
	}
}
