package com.emoon.tongue.controller;

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

import java.util.Map;

/**
 * 模型转发控制器：
 * - 公网入口：转发到内网 jar 的模型 controller
 * - 内网入口：真正调用 ai.llm.url 对应的模型网关
 */
@Slf4j
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequiredArgsConstructor
public class ModelRelayController {

    private final modelRelayService modelRelayService;

    @PostMapping({
        "/api/v1/model-relay/chat/completions",
        "/api/v1/forward/model-relay/chat/completions",
        "/v1/chat/completions"
    })
    public ResponseEntity<String> forwardChatCompletions(@RequestBody(required = false) Map<String, Object> requestBody) {
        return modelRelayService.forwardChatCompletions(requestBody);
    }

    /**
     * 内网真实模型入口：由公网 jar 转发调用，不建议前端直接调用。
     */
    @PostMapping("/api/v1/model-relay/internal/chat/completions")
    public ResponseEntity<String> callInternalChatCompletions(@RequestBody(required = false) Map<String, Object> requestBody) {
        return modelRelayService.callInternalChatCompletions(requestBody);
    }

    @GetMapping("/api/v1/model-relay/health")
    public R<String> health() {
        return R.ok("ok");
    }
}
