package com.infoepoch.pms.agent.web;

import com.infoepoch.pms.agent.service.PmsAgentService;
import com.infoepoch.pms.agent.web.dto.AgentChatRequest;
import com.infoepoch.pms.agent.web.dto.AgentChatResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
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 org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/agent")
public class AgentChatController {

    private final PmsAgentService pmsAgentService;

//    @PostMapping("/chat")
//    public ResponseEntity<AgentChatResponse> chat(@Valid @RequestBody AgentChatRequest request) {
//        return ResponseEntity.ok(pmsAgentService.chat(request));
//    }
//
//    @PostMapping(path = "/chat/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
//    public SseEmitter streamChat(@Valid @RequestBody AgentChatRequest request) {
//        return pmsAgentService.streamChat(request);
//    }

    @PostMapping("/chatTest")
    public void chatTest() {
        pmsAgentService.chatTest();
    }

    @PostMapping("/chatTest2")
    public void chatTest2() {
        pmsAgentService.chatTest1();
    }

    @PostMapping("/testChat3")
    public void testChat3() throws URISyntaxException {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        String url = "http://172.28.30.253:8001/v1/chat/completions";

        try {
            URI uri = new URI(url);
            ClientHttpRequest request = factory.createRequest(uri, HttpMethod.POST);
            // 4. 设置请求头
            request.getHeaders().set("Content-Type", "application/json");
            request.getHeaders().set("Accept", "application/json");
            request.getHeaders().set("Authorization", "Bearer 115e68c85d91d40dd3df42a06f08cbed");

            // 5. 准备请求体（例如 JSON 字符串）
            String requestBody = "{\n" +
                    "    \"model\": \"Qwen3.5-27B\",\n" +
                    "    \"messages\": [\n" +
                    "      {\"role\": \"user\", \"content\": \"你好\"}\n" +
                    "    ]\n" +
                    "}";

            // 6. 写入请求体
            try (OutputStream os = request.getBody()) {
                os.write(requestBody.getBytes(StandardCharsets.UTF_8));
                os.flush();
            }

            // 7. 执行请求并获取响应
            try (ClientHttpResponse response = request.execute()) {
                // 8. 处理响应
                HttpStatusCode statusCode = response.getStatusCode();
                System.out.println("Status Code: " + statusCode);

                // 读取响应体
                String responseBody = new String(response.getBody().readAllBytes(), StandardCharsets.UTF_8);
                System.out.println("Response Body: " + responseBody);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}