package com.rj.controller;

import com.rj.service.IQweiUserService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureWebMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
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
@AutoConfigureWebMvc
@ActiveProfiles("test")
class QweiUserControllerTest {

    @Autowired
    private WebApplicationContext webApplicationContext;

    @MockitoBean
    private IQweiUserService qweiUserService;

    private MockMvc mockMvc;

    @BeforeEach
    void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    void syncCustomerShouldReturnOkWhenServiceSuccess() throws Exception {
        Map<String, Object> serviceResult = new HashMap<>();
        serviceResult.put("success", true);
        serviceResult.put("message", "同步企微客户成功");
        serviceResult.put("inserted", 2);
        serviceResult.put("updated", 3);
        serviceResult.put("total", 5);
        when(qweiUserService.syncCustomerFromQiWei("TENANT_TEST")).thenReturn(serviceResult);

        mockMvc.perform(post("/api/qweiUser/syncCustomer")
                        .param("tenantId", "TENANT_TEST")
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andExpect(jsonPath("$.inserted").value(2))
                .andExpect(jsonPath("$.updated").value(3))
                .andExpect(jsonPath("$.total").value(5));

        verify(qweiUserService, times(1)).syncCustomerFromQiWei("TENANT_TEST");
    }

    @Test
    void syncCustomerShouldReturnBadRequestWhenServiceFailed() throws Exception {
        Map<String, Object> serviceResult = new HashMap<>();
        serviceResult.put("success", false);
        serviceResult.put("message", "同步企微客户异常：测试错误");
        when(qweiUserService.syncCustomerFromQiWei("TENANT_TEST")).thenReturn(serviceResult);

        mockMvc.perform(post("/api/qweiUser/syncCustomer")
                        .param("tenantId", "TENANT_TEST")
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isBadRequest())
                .andExpect(jsonPath("$.success").value(false));

        verify(qweiUserService, times(1)).syncCustomerFromQiWei("TENANT_TEST");
    }
}
