package cn.iocoder.yudao.module.infra.controller.app.file;

import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.infra.controller.app.file.vo.AppFileUploadReqVO;
import cn.iocoder.yudao.module.infra.controller.app.file.vo.MinioMultiUploadReqVO;
import cn.iocoder.yudao.module.infra.controller.app.file.vo.MinioUploadReqVO;
import cn.iocoder.yudao.module.infra.controller.app.file.vo.MinioUploadRespVO;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import cn.iocoder.yudao.module.infra.service.minio.MinioService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 App - 文件存储")
@RestController
@RequestMapping("/infra/file")
@Validated
@Slf4j
public class AppFileController {

    @Resource
    private FileService fileService;

    @Resource
    private MinioService minioService;

    @PostMapping("/upload")
    @Operation(summary = "上传文件")
    public CommonResult<String> uploadFile(AppFileUploadReqVO uploadReqVO) throws Exception {
        MultipartFile file = uploadReqVO.getFile();
        String path = uploadReqVO.getPath();
        return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
    }

    @PostMapping("/minio/upload")
    @Operation(summary = "MinIO单文件上传")
    public CommonResult<MinioUploadRespVO> minioUpload(MinioUploadReqVO uploadReqVO) {
        try {
            String fileUrl = minioService.uploadFile(uploadReqVO.getFile(), uploadReqVO.getPath());
            return success(MinioUploadRespVO.single(fileUrl));
        } catch (Exception e) {
            log.error("MinIO文件上传失败", e);
            return CommonResult.error(500, "文件上传失败: " + e.getMessage());
        }
    }

    @PostMapping("/minio/upload/multiple")
    @Operation(summary = "MinIO多文件上传")
    public CommonResult<MinioUploadRespVO> minioUploadMultiple(@RequestParam("files") MultipartFile[] files) {
        try {
            List<String> fileUrls = minioService.uploadFiles(files);
            return success(MinioUploadRespVO.multiple(fileUrls));
        } catch (Exception e) {
            log.error("MinIO多文件上传失败", e);
            return CommonResult.error(500, "文件上传失败: " + e.getMessage());
        }
    }

    @PostMapping("/minio/upload/batch")
    @Operation(summary = "MinIO批量文件上传（表单方式）")
    public CommonResult<MinioUploadRespVO> minioUploadBatch(MinioMultiUploadReqVO uploadReqVO) {
        try {
            List<String> fileUrls = minioService.uploadFiles(uploadReqVO.getFiles());
            return success(MinioUploadRespVO.multiple(fileUrls));
        } catch (Exception e) {
            log.error("MinIO批量文件上传失败", e);
            return CommonResult.error(500, "文件上传失败: " + e.getMessage());
        }
    }

}
