package cn.nianxx.thhotel.integrations.mcp.superagent.control;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import cn.nianxx.thhotel.ThHotelApplication;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;

@SpringBootTest(
        classes = ThHotelApplication.class,
        properties = {
                "mcp.enabled=true",
                "mcp.auth-token=test-mcp-token",
                "mcp.enable-submit-task-results=true",
                "mcp.max-body-bytes=12000"
        })
@AutoConfigureMockMvc
@ActiveProfiles("test")
class SuperAgentMcpSubmitEnabledControllerTest {

    private static final String ENDPOINT = "/mcp";
    private static final String AUTHORIZATION = "Bearer test-mcp-token";

    @Autowired
    private MockMvc mockMvc;

    @Test
    void shouldDelegateSubmitTaskResultsToolWhenWriteToolEnabled() throws Exception {
        String body = """
                {
                  "jsonrpc": "2.0",
                  "id": "mcp-submit-enabled-001",
                  "method": "tools/call",
                  "params": {
                    "name": "th_hotel_submit_task_results",
                    "arguments": {
                      "source_message_id": "mail-mcp-enabled-missing-001",
                      "ai_task_results": [
                        {
                          "source_event_index": 1,
                          "catalog_code": "S01",
                          "skill_id": "S01_new_booking_skill",
                          "result_type": "normal_task",
                          "task_type": "NEW_BOOKING"
                        }
                      ]
                    }
                  }
                }
                """;

        mockMvc.perform(post(ENDPOINT)
                        .contentType(MediaType.APPLICATION_JSON)
                        .header("Authorization", AUTHORIZATION)
                        .content(body))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.jsonrpc").value("2.0"))
                .andExpect(jsonPath("$.id").value("mcp-submit-enabled-001"))
                .andExpect(jsonPath("$.result.isError").value(true))
                .andExpect(jsonPath("$.result.structuredContent.error.code").value("SOURCE_MESSAGE_NOT_FOUND"))
                .andExpect(content().string(not(containsString("MCP_TOOL_DISABLED"))));
    }
}
