package com.rj.controller;

import com.rj.service.IQweiDepartmentService;
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")
public class QweiDepartmentControllerTest {

    @Autowired
    private WebApplicationContext webApplicationContext;

    @MockitoBean
    private IQweiDepartmentService qweiDepartmentService;

    private MockMvc mockMvc;

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

    @Test
    void syncEndpointShouldReturnOkWhenServiceSuccess() throws Exception {
        Map<String, Object> serviceResult = new HashMap<>();
        serviceResult.put("success", true);
        serviceResult.put("message", "同步成功");
        serviceResult.put("inserted", 3);
        serviceResult.put("updated", 5);
        serviceResult.put("total", 8);
        when(qweiDepartmentService.syncFromQiWei("TENANT_TEST")).thenReturn(serviceResult);

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

        verify(qweiDepartmentService, times(1)).syncFromQiWei("TENANT_TEST");
    }

    @Test
    void syncEndpointShouldReturnBadRequestWhenServiceFailed() throws Exception {
        Map<String, Object> serviceResult = new HashMap<>();
        serviceResult.put("success", false);
        serviceResult.put("message", "同步异常：测试错误");
        when(qweiDepartmentService.syncFromQiWei("TENANT_TEST")).thenReturn(serviceResult);

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

        verify(qweiDepartmentService, times(1)).syncFromQiWei("TENANT_TEST");
    }
}

