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

import cn.nianxx.thhotel.platform.debug.common.result.DebugEmlSuperAgentRunResult;
import cn.nianxx.thhotel.platform.debug.service.DebugEmlSuperAgentRunService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.HttpHeaders;
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;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

/**
 * Debug EML 上传 Controller。该入口只用于受控调试，不创建业务订单或任务。
 */
@RestController
@RequestMapping("/api/system/debug/eml-superagent-runs")
@ConditionalOnProperty(prefix = "debug.eml-upload", name = "enabled", havingValue = "true")
public class DebugEmlSuperAgentController {

    private final DebugEmlSuperAgentRunService runService;

    /**
     * 注入 Debug EML 服务，Controller 不直接访问 OSS、SuperAgent 或 Mapper。
     */
    public DebugEmlSuperAgentController(DebugEmlSuperAgentRunService runService) {
        this.runService = runService;
    }

    /**
     * 上传单封 .eml 邮件，写入 SourceMessage 后调用 SuperAgent Open API 并返回调试结果。
     */
    @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<DebugEmlSuperAgentRunResult> upload(
            @RequestHeader(name = "X-TH-Hotel-Debug-Upload-Key", required = false) String accessKey,
            @RequestParam("file") MultipartFile file,
            @RequestParam(name = "hotel_id", required = false) String hotelId,
            @RequestParam(name = "run_label", required = false) String runLabel) {
        return ResponseEntity.status(HttpStatus.CREATED).body(runService.uploadAndRun(accessKey, file, hotelId, runLabel));
    }

    /**
     * 上传单封 .eml 邮件并以 SSE 实时返回本系统阶段、SuperAgent 公开 Trace 和最终回答。
     */
    @PostMapping(path = "/stream", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public ResponseEntity<StreamingResponseBody> uploadStream(
            @RequestHeader(name = "X-TH-Hotel-Debug-Upload-Key", required = false) String accessKey,
            @RequestParam("file") MultipartFile file,
            @RequestParam(name = "hotel_id", required = false) String hotelId,
            @RequestParam(name = "run_label", required = false) String runLabel) {
        runService.validateUploadAccessKey(accessKey);
        StreamingResponseBody responseBody = outputStream ->
                runService.uploadAndRunStream(accessKey, file, hotelId, runLabel, outputStream);
        return ResponseEntity.ok()
                .header(HttpHeaders.CACHE_CONTROL, "no-cache")
                .header("X-Accel-Buffering", "no")
                .contentType(MediaType.TEXT_EVENT_STREAM)
                .body(responseBody);
    }
}
