package com.gxwebsoft.common.core.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxwebsoft.common.core.dto.qr.CreateEncryptedQrCodeRequest;
import com.gxwebsoft.common.core.utils.EncryptedQrCodeUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

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

import static org.mockito.ArgumentMatchers.*;
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.*;

/**
 * QR码控制器测试
 *
 * @author WebSoft
 * @since 2025-08-18
 */
@WebMvcTest(QrCodeController.class)
public class QrCodeControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private EncryptedQrCodeUtil encryptedQrCodeUtil;

    @Autowired
    private ObjectMapper objectMapper;

    @Test
    public void testCreateEncryptedQrCodeWithValidRequest() throws Exception {
        // 准备测试数据
        CreateEncryptedQrCodeRequest request = new CreateEncryptedQrCodeRequest();
        request.setData("test data");
        request.setWidth(200);
        request.setHeight(200);
        request.setExpireMinutes(30L);
        request.setBusinessType("TEST");

        Map<String, Object> mockResult = new HashMap<>();
        mockResult.put("qrCodeBase64", "base64_encoded_image");
        mockResult.put("token", "test_token");

        when(encryptedQrCodeUtil.generateEncryptedQrCode(anyString(), anyInt(), anyInt(), anyLong(), anyString()))
                .thenReturn(mockResult);

        // 执行测试
        mockMvc.perform(post("/api/qr-code/create-encrypted-qr-code")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(request)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(200))
                .andExpect(jsonPath("$.message").value("生成加密二维码成功"))
                .andExpect(jsonPath("$.data.qrCodeBase64").value("base64_encoded_image"))
                .andExpect(jsonPath("$.data.token").value("test_token"));
    }

    @Test
    public void testCreateEncryptedQrCodeWithInvalidRequest() throws Exception {
        // 准备无效的测试数据（data为空）
        CreateEncryptedQrCodeRequest request = new CreateEncryptedQrCodeRequest();
        request.setData(""); // 空字符串，应该触发验证失败
        request.setWidth(200);
        request.setHeight(200);
        request.setExpireMinutes(30L);

        // 执行测试
        mockMvc.perform(post("/api/qr-code/create-encrypted-qr-code")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(request)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(500))
                .andExpect(jsonPath("$.message").value("数据不能为空"));
    }

    @Test
    public void testCreateEncryptedQrCodeWithInvalidSize() throws Exception {
        // 准备无效的测试数据（尺寸超出范围）
        CreateEncryptedQrCodeRequest request = new CreateEncryptedQrCodeRequest();
        request.setData("test data");
        request.setWidth(2000); // 超出最大值1000
        request.setHeight(200);
        request.setExpireMinutes(30L);

        // 执行测试
        mockMvc.perform(post("/api/qr-code/create-encrypted-qr-code")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(request)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(500))
                .andExpect(jsonPath("$.message").value("宽度不能大于1000像素"));
    }
}
