package com.emoon.tongue.controller;

import com.emoon.common.core.domain.R;
import com.emoon.tongue.service.ForwardRobotService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 转发控制器（机器人端）：用于公网部署时，将请求原样转发到内网的同名服务。
 *
 * 注意：
 * - 只做"转发网关"职责：不改参数、不改签名逻辑；签名校验由内网服务自己完成。
 * - 新入口路径可以变，但参数名/请求体与原 DiagnosisRobotController 保持一致。
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/forward/diagnosis/robot")
@CrossOrigin(origins = "*", maxAge = 3600)
@RequiredArgsConstructor
public class ForwardRobotController {

    private final ForwardRobotService forwardRobotService;

    /**
     * 转发：舌象图片校验（multipart/form-data）
     * 原接口：POST /api/v1/diagnosis/robot/check-image
     */
    @PostMapping("/check-image")
    public ResponseEntity<String> checkTongueImage(@RequestParam("file") MultipartFile file,
                                                   @RequestParam String patientId,
                                                   @RequestParam String projectId,
                                                   @RequestParam Long timestamp,
                                                   @RequestParam String sign,
                                                   @RequestParam String version,
                                                   @RequestParam String bizParams) {
        return forwardRobotService.forwardCheckImage(file, patientId, projectId, timestamp, sign, version, bizParams);
    }

    /**
     * 转发：获取舌诊详情
     * 原接口：GET /api/v1/diagnosis/robot/detail
     */
    @GetMapping("/detail")
    public ResponseEntity<String> getDiagnosisDetail(@RequestParam String patientId,
                                                     @RequestParam String projectId,
                                                     @RequestParam Long timestamp,
                                                     @RequestParam String version,
                                                     @RequestParam String sign) {
        return forwardRobotService.forwardDetail(patientId, projectId, timestamp, version, sign);
    }

    /**
     * 转发：启动 AI 诊断
     * 原接口：POST /api/v1/diagnosis/robot/ai-diagnosis
     */
    @PostMapping("/ai-diagnosis")
    public ResponseEntity<String> performAiDiagnosis(@RequestParam String patientId,
                                                     @RequestParam String projectId,
                                                     @RequestParam Long timestamp,
                                                     @RequestParam String version,
                                                     @RequestParam String sign) {
        return forwardRobotService.forwardAiDiagnosis(patientId, projectId, timestamp, version, sign);
    }

    /**
     * 转发：LLM 调用
     * 原接口：POST /api/v1/diagnosis/robot/llm-call
     */
    @PostMapping("/llm-call")
    public ResponseEntity<String> callLlm(@RequestBody Map<String, String> request) {
        return forwardRobotService.forwardJsonPost("/api/v1/diagnosis/robot/llm-call", request);
    }

    /**
     * 转发：生成签名
     * 原接口：POST /api/v1/diagnosis/robot/generate-sign
     */
    @PostMapping("/generate-sign")
    public ResponseEntity<String> generateSignForFrontend(@RequestBody Map<String, Object> params) {
        return forwardRobotService.forwardJsonPost("/api/v1/diagnosis/robot/generate-sign", params);
    }

    /**
     * 快速健康检查：便于确认转发目标是否可达
     */
    @GetMapping("/health")
    public R<String> health() {
        return R.ok("ok");
    }
}




