package incheon.sgp.ipd.llm.web;

import incheon.sgp.ipd.llm.service.LlmService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Tag(name = "LLM 챗봇 API", description = "LLM 챗봇 프록시 API")
@RestController
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/api/v1/llm")
public class LlmApiController {

    private final LlmService llmService;

    @Operation(summary = "챗봇 대화", description = "LLM 서버로 질문을 전달하고 응답을 반환합니다.")
    @PostMapping("/chat")
    public ResponseEntity<Map<String, Object>> chat(@RequestBody Map<String, Object> request) {
        try {
            Map<String, Object> response = llmService.chat(request);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("LLM 챗봇 API 오류: {}", e.getMessage(), e);
            return ResponseEntity.internalServerError()
                .body(Map.of(
                    "error", true,
                    "message", "챗봇 서버 연결에 실패했습니다."
                ));
        }
    }
}
