package admin

import (
	"github.com/gofiber/fiber/v2"
	"go.uber.org/zap"

	"github.com/break/junhong_cmp_fiber/internal/model/dto"
	"github.com/break/junhong_cmp_fiber/pkg/errors"
	"github.com/break/junhong_cmp_fiber/pkg/logger"
	"github.com/break/junhong_cmp_fiber/pkg/response"
	"github.com/break/junhong_cmp_fiber/pkg/storage"
)

type StorageHandler struct {
	service *storage.Service
}

func NewStorageHandler(service *storage.Service) *StorageHandler {
	return &StorageHandler{service: service}
}

func (h *StorageHandler) GetUploadURL(c *fiber.Ctx) error {
	if h.service == nil {
		return errors.New(errors.CodeInternalError, "对象存储服务未配置")
	}

	var req dto.GetUploadURLRequest
	if err := c.BodyParser(&req); err != nil {
		return errors.New(errors.CodeInvalidParam, "请求参数解析失败")
	}

	result, err := h.service.GetUploadURL(c.UserContext(), req.Purpose, req.FileName, req.ContentType)
	if err != nil {
		logger.GetAppLogger().Error("获取上传URL失败",
			zap.String("path", c.Path()),
			zap.String("method", c.Method()),
			zap.String("purpose", req.Purpose),
			zap.String("fileName", req.FileName),
			zap.Error(err),
		)
		return errors.New(errors.CodeInternalError, "获取上传URL失败")
	}

	return response.Success(c, dto.GetUploadURLResponse{
		UploadURL: result.URL,
		FileKey:   result.FileKey,
		ExpiresIn: result.ExpiresIn,
	})
}
