package common

import (
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"github.com/perfect-panel/server/internal/logic/common"
	"github.com/perfect-panel/server/internal/svc"
	"github.com/perfect-panel/server/internal/types"
	"github.com/perfect-panel/server/pkg/result"
	"github.com/perfect-panel/server/pkg/xerr"
	"github.com/pkg/errors"
)

// GetDownloadLinkHandler 获取下载链接
func GetDownloadLinkHandler(svcCtx *svc.ServiceContext) func(c *gin.Context) {
	return func(c *gin.Context) {
		var req types.GetDownloadLinkRequest
		if err := c.ShouldBind(&req); err != nil {
			result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "parse params failed: %v", err))
			return
		}
		validate := validator.New()
		if err := validate.Struct(&req); err != nil {
			result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidParams), "validate params failed: %v", err))
			return
		}

		l := common.NewGetDownloadLinkLogic(c.Request.Context(), svcCtx)
		resp, err := l.GetDownloadLink(&req)
		result.HttpResult(c, resp, err)
	}
}
