package com.nanri.aiimage.modules.file.controller;

import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.modules.file.model.vo.ExcelInfoVo;
import com.nanri.aiimage.modules.file.model.vo.UploadFileVo;
import com.nanri.aiimage.modules.file.service.LocalFileStorageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
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;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/files")
@Tag(name = "文件上传", description = "用于前端将本地文件上传到后端临时目录，后续任务接口基于 fileKey 进行处理")
public class FileUploadController {

    private final LocalFileStorageService localFileStorageService;

    @PostMapping("/upload")
    @Operation(
            summary = "上传临时文件",
            description = """
                    上传本地 Excel 文件到后端临时目录，返回 fileKey。

                    该接口不是给浏览器页面直接使用的最终业务接口，而是给 Python 桌面端壳做文件中转：
                    - 桌面端先选本地文件；
                    - 再调用本接口上传到 Java 临时目录；
                    - 然后把 fileKey 传给 dedupe/convert/split 执行接口；
                    - 处理完成后，桌面端再调用下载接口把结果文件保存回用户本地目录。
                    """)
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "上传成功，返回临时文件信息", content = @Content(schema = @Schema(implementation = UploadFileVo.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "未传文件或文件不合法"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器保存临时文件失败")
    })
    public ApiResponse<UploadFileVo> upload(
            @Parameter(name = "file", description = "待上传的 Excel 或业务源文件", required = true, in = ParameterIn.QUERY)
            MultipartFile file,
            @RequestParam(required = false) String relativePath) throws Exception {
        return ApiResponse.success(localFileStorageService.saveTempFile(file, relativePath));
    }

    @GetMapping("/excel-info")
    @Operation(summary = "读取 Excel 信息", description = "根据 fileKey 读取 Excel 第一行表头及总行数，供前端选择保留列和展示总条数。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "读取成功", content = @Content(schema = @Schema(implementation = ExcelInfoVo.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "源文件不存在")
    })
    public ApiResponse<ExcelInfoVo> excelInfo(@RequestParam String fileKey) throws Exception {
        return ApiResponse.success(localFileStorageService.getExcelInfo(fileKey));
    }
}
