package com.jeesharp.business.file.controller;

import com.jeesharp.framework.file.options.UploadOptions;
import com.jeesharp.framework.file.result.UploadResult;
import com.jeesharp.framework.file.service.FileStorageService;
import com.jeesharp.framework.memberauth.web.BaseMobileController;
import com.jeesharp.framework.pojo.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 移动端文件上传接口
 *
 * @author jeesharp
 * @version 2026-02-05
 */
@Slf4j
@RestController
@RequestMapping("${jeesharp.web.mobilePath}/file")
@Tag(name = "移动端-文件上传", description = "文件上传相关接口")
@AllArgsConstructor
public class FileMobileController extends BaseMobileController {

    @Autowired
    private FileStorageService fileStorageService;

    @Operation(
            summary = "文件上传",
            description = "上传单个文件，返回包含相对路径、文件名称、文件类型等信息的对象"
    )
    @PostMapping({"/uploadFile"})
    public AjaxResult<UploadResult> uploadFile(@Parameter(description = "上传的文件",required = true) @RequestParam("file") MultipartFile file, @Parameter(description = "存储桶/目录名") @RequestParam(defaultValue = "temp") String bucket, @Parameter(description = "关联业务ID") @RequestParam(required = false) String objectId, @Parameter(description = "对象类型") @RequestParam(required = false) String objectType) {
        UploadOptions opts = null;
        if (objectId != null || objectType != null) {
            opts = UploadOptions.builder().objectId(objectId).objectType(objectType).build();
        }
        UploadResult result = this.fileStorageService.uploadWithResult(file, bucket, opts);
        return AjaxResult.okData(result);
    }

    @Operation(
            summary = "文件上传",
            description = "上传单个文件，返回包含相对路径、文件名称、文件类型等信息的对象"
    )
    @PostMapping({"/uploadImage"})
    public AjaxResult<UploadResult> uploadImage(@Parameter(description = "上传的文件",required = true) @RequestParam("file") MultipartFile file, @Parameter(description = "存储桶/目录名") @RequestParam(defaultValue = "temp") String bucket, @Parameter(description = "关联业务ID") @RequestParam(required = false) String objectId, @Parameter(description = "对象类型") @RequestParam(required = false) String objectType) {
        UploadOptions opts = null;
        if (objectId != null || objectType != null) {
            opts = UploadOptions.builder().objectId(objectId).objectType(objectType).build();
        }
        UploadResult result = this.fileStorageService.uploadWithResult(file, bucket, opts);
        return AjaxResult.okData(result);
    }

}
