package app

import (
	"git.echol.cn/loser/st/server/global"
	commonResponse "git.echol.cn/loser/st/server/model/common/response"
	"git.echol.cn/loser/st/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type UploadApi struct{}

// UploadImage 上传图片
// @Summary 上传图片
// @Tags Upload
// @Accept multipart/form-data
// @Produce json
// @Param file formData file true "图片文件"
// @Success 200 {object} map[string]string "返回图片URL"
// @Router /app/upload/image [post]
func (a *UploadApi) UploadImage(c *gin.Context) {
	_, header, err := c.Request.FormFile("file")
	if err != nil {
		global.GVA_LOG.Error("接收文件失败", zap.Error(err))
		commonResponse.FailWithMessage("请上传图片文件", c)
		return
	}

	// 上传图片到 OSS
	imageURL, err := service.ServiceGroupApp.AppServiceGroup.UploadService.UploadImage(header)
	if err != nil {
		global.GVA_LOG.Error("上传图片失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	// 返回图片 URL
	commonResponse.OkWithData(gin.H{
		"url": imageURL,
	}, c)
}
