package com.sino.mci.flow.controller;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

import static com.sino.mci.test.AuthTestHelper.createTenantAndLogin;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@Transactional
public class IvrFlowControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void shouldManageIvrFlowLifecycle() throws Exception {
        String tenantCode = "ivr-" + UUID.randomUUID().toString().substring(0, 8);
        String authToken = createTenantAndLogin(mockMvc, tenantCode, "IVR 测试租户");

        String flowPayload = "{\"flowCode\":\"ivr_default\",\"flowName\":\"默认 IVR 流程\",\"flowType\":\"ivr\",\"definitionJson\":{\"nodes\":[{\"id\":\"start\",\"type\":\"start\",\"config\":{\"triggerType\":\"inbound_call\",\"phoneNumber\":\"10086\"}},{\"id\":\"menu\",\"type\":\"menu\",\"config\":{\"prompt\":\"按 1 转人工\",\"timeout\":10,\"options\":[{\"key\":\"1\",\"label\":\"人工\"}]}}],\"edges\":[]}}";

        mockMvc.perform(post("/msg-platform/api/v1/flow")
                        .contextPath("/msg-platform")
                        .header("Authorization", authToken)
                        .header("X-Tenant-Code", tenantCode)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(flowPayload))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(0))
                .andExpect(jsonPath("$.data.flowType").value(2));

        mockMvc.perform(get("/msg-platform/api/v1/flow/list")
                        .contextPath("/msg-platform")
                        .header("Authorization", authToken)
                        .header("X-Tenant-Code", tenantCode))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(0))
                .andExpect(jsonPath("$.data.length()").value(1))
                .andExpect(jsonPath("$.data[0].flowCode").value("ivr_default"));

        mockMvc.perform(get("/msg-platform/api/v1/flow/ivr_default")
                        .contextPath("/msg-platform")
                        .header("Authorization", authToken)
                        .header("X-Tenant-Code", tenantCode))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(0))
                .andExpect(jsonPath("$.data.flowCode").value("ivr_default"));

        mockMvc.perform(post("/msg-platform/api/v1/flow/ivr_default/publish")
                        .contextPath("/msg-platform")
                        .header("Authorization", authToken)
                        .header("X-Tenant-Code", tenantCode))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(0));

        mockMvc.perform(get("/msg-platform/api/v1/flow/ivr_default")
                        .contextPath("/msg-platform")
                        .header("Authorization", authToken)
                        .header("X-Tenant-Code", tenantCode))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.data.status").value(1));

        mockMvc.perform(delete("/msg-platform/api/v1/flow/ivr_default")
                        .contextPath("/msg-platform")
                        .header("Authorization", authToken)
                        .header("X-Tenant-Code", tenantCode))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(0));
    }
}
