package com.lingyue.parse.controller;

import com.lingyue.common.domain.AjaxResult;
import com.lingyue.parse.dto.FileUploadResponse;
import com.lingyue.parse.service.FileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传控制器
 * 
 * @author lingyue
 * @since 2026-01-14
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/parse")
@RequiredArgsConstructor
@Tag(name = "文件上传", description = "文件上传相关接口")
public class FileUploadController {
    
    private final FileUploadService fileUploadService;
    
    @PostMapping("/upload")
    @Operation(summary = "上传文件", description = "上传PDF、Word、Excel或图片文件")
    public AjaxResult<FileUploadResponse> uploadFile(
            @Parameter(description = "上传的文件", required = true)
            @RequestParam("file") MultipartFile file,
            
            @Parameter(description = "用户ID", required = true)
            @RequestParam("userId") String userId) {
        
        log.info("接收文件上传请求, 文件名: {}, 大小: {} bytes, 用户ID: {}", 
                file.getOriginalFilename(), file.getSize(), userId);
        
        FileUploadResponse response = fileUploadService.uploadFile(file, userId);
        
        log.info("文件上传成功, 文档ID: {}", response.getDocumentId());
        
        return AjaxResult.success(response);
    }
    
    @GetMapping("/file/exists")
    @Operation(summary = "检查文件是否存在")
    public AjaxResult<Boolean> checkFileExists(
            @Parameter(description = "文件路径", required = true)
            @RequestParam("filePath") String filePath) {
        
        boolean exists = fileUploadService.fileExists(filePath);
        return AjaxResult.success(exists);
    }
    
    @DeleteMapping("/file")
    @Operation(summary = "删除文件")
    public AjaxResult<Void> deleteFile(
            @Parameter(description = "文件路径", required = true)
            @RequestParam("filePath") String filePath) {
        
        fileUploadService.deleteFile(filePath);
        return AjaxResult.success("删除成功", null);
    }
}
