package shop.alien.dining.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import shop.alien.entity.result.R;
import shop.alien.dining.feign.AlienStoreFeign;

/**
 * 点餐模块-文件上传控制器
 *
 * @author ssk
 * @version 1.0
 * @date 2025/01/XX
 */
@Slf4j
@Api(tags = {"小程序-文件上传"})
@ApiSort(10)
@CrossOrigin
@RestController
@RequestMapping("/dining/file")
@RequiredArgsConstructor
public class DiningFileUploadController {

    private final AlienStoreFeign alienStoreFeign;

    /**
     * 上传图片到OSS（单个文件上传，支持图片、视频或PDF）
     *
     * @param file 文件对象
     * @return R.data 为文件路径（String）
     */
    @ApiOperation(value = "上传图片到OSS", notes = "支持图片、视频或PDF文件上传，返回OSS文件路径")
    @ApiOperationSupport(order = 1)
    @PostMapping("/upload")
    public R<String> upload(@RequestParam("file") MultipartFile file) {
        log.info("DiningFileUploadController.upload?fileName={}", file != null ? file.getOriginalFilename() : null);
        try {
            if (file == null || file.isEmpty()) {
                return R.fail("文件不能为空");
            }
            R<String> result = alienStoreFeign.uploadFile(file);
            if (result.isSuccess()) {
                log.info("文件上传成功，OSS路径：{}", result.getData());
                return result;
            } else {
                log.error("文件上传失败：{}", result.getMsg());
                return R.fail("文件上传失败：" + result.getMsg());
            }
        } catch (Exception e) {
            log.error("文件上传异常：{}", e.getMessage(), e);
            return R.fail("文件上传异常：" + e.getMessage());
        }
    }
}
