package http

import (
	"mime/multipart"
	"net/http"
	"net/url"

	"github.com/gin-gonic/gin"
	"gitlink.org.cn/cloudream/common/consts/errorcode"
	"gitlink.org.cn/cloudream/common/pkgs/iterator"
	"gitlink.org.cn/cloudream/common/pkgs/logger"
	"gitlink.org.cn/cloudream/common/sdks/storage/cdsapi"

	stgiter "gitlink.org.cn/cloudream/storage/common/pkgs/iterator"
)

// PackageService 包服务，负责处理包相关的HTTP请求。
type PackageService struct {
	*Server
}

// Package 返回PackageService的实例。
func (s *Server) Package() *PackageService {
	return &PackageService{
		Server: s,
	}
}

func (s *PackageService) Get(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.Get")

	var req cdsapi.PackageGetReq
	if err := ctx.ShouldBindQuery(&req); err != nil {
		log.Warnf("binding body: %s", err.Error())
		ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
		return
	}

	pkg, err := s.svc.PackageSvc().Get(req.UserID, req.PackageID)
	if err != nil {
		log.Warnf("getting package: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get package failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(cdsapi.PackageGetResp{Package: *pkg}))
}

func (s *PackageService) GetByName(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.GetByName")

	var req cdsapi.PackageGetByName
	if err := ctx.ShouldBindQuery(&req); err != nil {
		log.Warnf("binding query: %s", err.Error())
		ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
		return
	}

	pkg, err := s.svc.PackageSvc().GetByName(req.UserID, req.BucketName, req.PackageName)
	if err != nil {
		log.Warnf("getting package by name: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get package by name failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(cdsapi.PackageGetByNameResp{Package: *pkg}))
}

// Create 处理创建新包的HTTP请求。
func (s *PackageService) Create(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.Create")
	var req cdsapi.PackageCreate
	if err := ctx.ShouldBindJSON(&req); err != nil {
		log.Warnf("binding body: %s", err.Error())
		ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
		return
	}

	pkg, err := s.svc.PackageSvc().Create(req.UserID, req.BucketID, req.Name)
	if err != nil {
		log.Warnf("creating package: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "create package failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(cdsapi.PackageCreateResp{
		Package: pkg,
	}))
}

func (s *PackageService) Delete(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.Delete")

	var req cdsapi.PackageDelete
	if err := ctx.ShouldBindJSON(&req); err != nil {
		log.Warnf("binding body: %s", err.Error())
		ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
		return
	}

	err := s.svc.PackageSvc().DeletePackage(req.UserID, req.PackageID)
	if err != nil {
		log.Warnf("deleting package: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "delete package failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(nil))
}

func (s *PackageService) ListBucketPackages(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.ListBucketPackages")

	var req cdsapi.PackageListBucketPackages
	if err := ctx.ShouldBindQuery(&req); err != nil {
		log.Warnf("binding query: %s", err.Error())
		ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
		return
	}

	pkgs, err := s.svc.PackageSvc().GetBucketPackages(req.UserID, req.BucketID)
	if err != nil {
		log.Warnf("getting bucket packages: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get bucket packages failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(cdsapi.PackageListBucketPackagesResp{
		Packages: pkgs,
	}))
}

// GetCachedStorages 处理获取包的缓存节点的HTTP请求。
func (s *PackageService) GetCachedStorages(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.GetCachedStorages")

	var req cdsapi.PackageGetCachedStoragesReq
	if err := ctx.ShouldBindQuery(&req); err != nil {
		log.Warnf("binding query: %s", err.Error())
		ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
		return
	}

	resp, err := s.svc.PackageSvc().GetCachedStorages(req.UserID, req.PackageID)
	if err != nil {
		log.Warnf("get package cached storages failed: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get package cached storages failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(cdsapi.PackageGetCachedStoragesResp{PackageCachingInfo: resp}))
}

// GetLoadedStorages 处理获取包的加载节点的HTTP请求。
func (s *PackageService) GetLoadedStorages(ctx *gin.Context) {
	log := logger.WithField("HTTP", "Package.GetLoadedStorages")

	var req cdsapi.PackageGetLoadedStoragesReq
	if err := ctx.ShouldBindQuery(&req); err != nil {
		log.Warnf("binding query: %s", err.Error())
		ctx.JSON(http.StatusBadRequest, Failed(errorcode.BadArgument, "missing argument or invalid argument"))
		return
	}

	stgIDs, err := s.svc.PackageSvc().GetLoadedStorages(req.UserID, req.PackageID)
	if err != nil {
		log.Warnf("get package loaded storages failed: %s", err.Error())
		ctx.JSON(http.StatusOK, Failed(errorcode.OperationFailed, "get package loaded storages failed"))
		return
	}

	ctx.JSON(http.StatusOK, OK(cdsapi.PackageGetLoadedStoragesResp{
		StorageIDs: stgIDs,
	}))
}

// mapMultiPartFileToUploadingObject 将multipart文件转换为上传对象的迭代器。
func mapMultiPartFileToUploadingObject(files []*multipart.FileHeader) stgiter.UploadingObjectIterator {
	return iterator.Map[*multipart.FileHeader](
		iterator.Array(files...),
		func(file *multipart.FileHeader) (*stgiter.IterUploadingObject, error) {
			stream, err := file.Open()
			if err != nil {
				return nil, err
			}

			fileName, err := url.PathUnescape(file.Filename)
			if err != nil {
				return nil, err
			}

			return &stgiter.IterUploadingObject{
				Path: fileName,
				Size: file.Size,
				File: stream,
			}, nil
		},
	)
}
