package cn.nianxx.thhotel.platform.documentconversion.control;

import cn.nianxx.thhotel.platform.documentconversion.common.result.ExcelToPdfConversionResult;
import cn.nianxx.thhotel.platform.documentconversion.service.DocumentConversionService;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
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;

/**
 * 文件转换 Controller。当前只开放受控 Excel 转 PDF 手动上传接口。
 */
@RestController
@RequestMapping("/api/system/document-conversions")
public class DocumentConversionController {

    private final DocumentConversionService documentConversionService;

    /**
     * 注入文件转换服务，Controller 不直接调用 LibreOffice 或 OSS。
     */
    public DocumentConversionController(DocumentConversionService documentConversionService) {
        this.documentConversionService = documentConversionService;
    }

    /**
     * 上传 Excel 并转换为 PDF，成功后返回 PDF OSS URL。
     */
    @PostMapping(path = "/excel-to-pdf", consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ExcelToPdfConversionResult> convertExcelToPdf(
            @RequestHeader(name = "X-TH-Hotel-Document-Conversion-Key", required = false) String accessKey,
            @RequestParam("file") MultipartFile file,
            @RequestParam(name = "hotel_id", required = false) String hotelId) {
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(documentConversionService.convertExcelToPdf(accessKey, file, hotelId));
    }
}
