package v1

import (
	"mime/multipart"

	"quyun/v2/app/http/v1/dto"
	"quyun/v2/app/services"
	"quyun/v2/database/models"

	"github.com/gofiber/fiber/v3"
)

// @provider
type Common struct{}

// @Router /t/:tenantCode/v1/upload [post]
// @Summary Upload file
// @Description Upload file
// @Tags Common
// @Accept multipart/form-data
// @Produce json
// @Param file formData file true "File"
// @Param form formData dto.UploadForm true "Upload form"
// @Success 200 {object} dto.UploadResult
// @Bind user local key(__ctx_user)
// @Bind file file
// @Bind form body
func (c *Common) Upload(
	ctx fiber.Ctx,
	user *models.User,
	file *multipart.FileHeader,
	form *dto.UploadForm,
) (*dto.UploadResult, error) {
	tenantID := getTenantID(ctx)
	val := ""
	if form != nil {
		val = form.Type
	}
	return services.Common.Upload(ctx, tenantID, user.ID, file, val)
}

// Get options (enums)
//
// @Router /t/:tenantCode/v1/common/options [get]
// @Summary Get options
// @Description Get global options (enums)
// @Tags Common
// @Accept json
// @Produce json
// @Success 200 {object} dto.OptionsResponse
func (c *Common) GetOptions(ctx fiber.Ctx) (*dto.OptionsResponse, error) {
	return services.Common.Options(ctx)
}

// Check file hash for deduplication
//
// @Router /t/:tenantCode/v1/upload/check [get]
// @Summary Check hash
// @Description Check if file hash exists
// @Tags Common
// @Accept json
// @Produce json
// @Param hash query string true "File MD5 Hash"
// @Success 200 {object} dto.UploadResult
// @Bind user local key(__ctx_user)
// @Bind hash query
func (c *Common) CheckHash(ctx fiber.Ctx, user *models.User, hash string) (*dto.UploadResult, error) {
	tenantID := getTenantID(ctx)
	return services.Common.CheckHash(ctx, tenantID, user.ID, hash)
}

// @Router /t/:tenantCode/v1/upload/init [post]
// @Summary Init multipart upload
// @Description Initialize multipart upload
// @Tags Common
// @Accept json
// @Produce json
// @Param form body dto.UploadInitForm true "Init form"
// @Success 200 {object} dto.UploadInitResponse
// @Bind user local key(__ctx_user)
// @Bind form body
func (c *Common) InitUpload(ctx fiber.Ctx, user *models.User, form *dto.UploadInitForm) (*dto.UploadInitResponse, error) {
	tenantID := getTenantID(ctx)
	return services.Common.InitUpload(ctx.Context(), tenantID, user.ID, form)
}

// @Router /t/:tenantCode/v1/upload/part [post]
// @Summary Upload part
// @Description Upload a part
// @Tags Common
// @Accept multipart/form-data
// @Produce json
// @Param file formData file true "Part File"
// @Param form formData dto.UploadPartForm true "Part form"
// @Success 200 {string} string "OK"
// @Bind user local key(__ctx_user)
// @Bind file file
// @Bind form body
func (c *Common) UploadPart(ctx fiber.Ctx, user *models.User, file *multipart.FileHeader, form *dto.UploadPartForm) error {
	tenantID := getTenantID(ctx)
	return services.Common.UploadPart(ctx.Context(), tenantID, user.ID, file, form)
}

// @Router /t/:tenantCode/v1/upload/complete [post]
// @Summary Complete upload
// @Description Complete multipart upload
// @Tags Common
// @Accept json
// @Produce json
// @Param form body dto.UploadCompleteForm true "Complete form"
// @Success 200 {object} dto.UploadResult
// @Bind user local key(__ctx_user)
// @Bind form body
func (c *Common) CompleteUpload(ctx fiber.Ctx, user *models.User, form *dto.UploadCompleteForm) (*dto.UploadResult, error) {
	tenantID := getTenantID(ctx)
	return services.Common.CompleteUpload(ctx.Context(), tenantID, user.ID, form)
}

// @Router /t/:tenantCode/v1/upload/:uploadId [delete]
// @Summary Abort upload
// @Description Abort multipart upload
// @Tags Common
// @Accept json
// @Produce json
// @Param uploadId path string true "Upload ID"
// @Success 200 {string} string "OK"
// @Bind user local key(__ctx_user)
// @Bind uploadId path
func (c *Common) AbortUpload(ctx fiber.Ctx, user *models.User, uploadId string) error {
	tenantID := getTenantID(ctx)
	return services.Common.AbortUpload(ctx.Context(), tenantID, user.ID, uploadId)
}

// @Router /t/:tenantCode/v1/media-assets/:id<int> [delete]
// @Summary Delete media asset
// @Description Delete media asset
// @Tags Common
// @Accept json
// @Produce json
// @Param id path int64 true "Asset ID"
// @Success 200 {string} string "OK"
// @Bind user local key(__ctx_user)
// @Bind id path
func (c *Common) DeleteMediaAsset(ctx fiber.Ctx, user *models.User, id int64) error {
	tenantID := getTenantID(ctx)
	return services.Common.DeleteMediaAsset(ctx.Context(), tenantID, user.ID, id)
}

// Upload file
