package top.continew.admin.auth;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
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.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import top.continew.admin.auth.model.req.UserRegisterReq;
import top.continew.admin.common.enums.GenderEnum;
import top.continew.admin.common.util.SecureUtils;

/**
 * 用户注册功能测试
 *
 * @author Charles7c
 * @since 2024/12/04 10:00
 */
@SpringBootTest
@AutoConfigureWebMvc
@ActiveProfiles("test")
public class UserRegisterTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testUserRegister() throws Exception {
        // 创建注册请求
        UserRegisterReq registerReq = new UserRegisterReq();
        registerReq.setUsername("testuser" + System.currentTimeMillis());
        registerReq.setNickname("测试用户");

        // 加密密码
        String password = "Test123456";
        String encryptedPassword = SecureUtils.encryptByRsaPublicKey(password);
        registerReq.setPassword(encryptedPassword);
        registerReq.setConfirmPassword(encryptedPassword);

        registerReq.setEmail("test" + System.currentTimeMillis() + "@example.com");
        registerReq.setPhone("138" + String.format("%08d", System.currentTimeMillis() % 100000000));
        registerReq.setGender(GenderEnum.MALE);
        registerReq.setCaptcha("1234"); // 测试验证码
        registerReq.setUuid(IdUtil.fastUUID());

        // 执行注册请求
        mockMvc.perform(MockMvcRequestBuilders.post("/auth/register")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JSONUtil.toJsonStr(registerReq)))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.jsonPath("$.token").exists())
            .andExpect(MockMvcResultMatchers.jsonPath("$.aiBotToken").exists());
    }

    @Test
    public void testPhoneRegister() throws Exception {
        // 创建手机号注册请求
        top.continew.admin.auth.model.req.PhoneRegisterReq registerReq = new top.continew.admin.auth.model.req.PhoneRegisterReq();

        registerReq.setPhone("139" + String.format("%08d", System.currentTimeMillis() % 100000000));
        registerReq.setNickname("手机用户");

        // 加密密码
        String password = "Test123456";
        String encryptedPassword = SecureUtils.encryptByRsaPublicKey(password);
        registerReq.setPassword(encryptedPassword);
        registerReq.setConfirmPassword(encryptedPassword);

        registerReq.setGender(GenderEnum.FEMALE);
        registerReq.setCaptcha("8888"); // 测试验证码

        // 执行注册请求
        mockMvc.perform(MockMvcRequestBuilders.post("/auth/register/phone")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JSONUtil.toJsonStr(registerReq)))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.jsonPath("$.token").exists())
            .andExpect(MockMvcResultMatchers.jsonPath("$.aiBotToken").exists());
    }

    @Test
    public void testEmailRegister() throws Exception {
        // 创建邮箱注册请求
        top.continew.admin.auth.model.req.EmailRegisterReq registerReq = new top.continew.admin.auth.model.req.EmailRegisterReq();

        registerReq.setEmail("email" + System.currentTimeMillis() + "@test.com");
        registerReq.setNickname("邮箱用户");

        // 加密密码
        String password = "Test123456";
        String encryptedPassword = SecureUtils.encryptByRsaPublicKey(password);
        registerReq.setPassword(encryptedPassword);
        registerReq.setConfirmPassword(encryptedPassword);

        registerReq.setGender(GenderEnum.UNKNOWN);
        registerReq.setCaptcha("888888"); // 测试验证码

        // 执行注册请求
        mockMvc.perform(MockMvcRequestBuilders.post("/auth/register/email")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JSONUtil.toJsonStr(registerReq)))
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(MockMvcResultMatchers.jsonPath("$.token").exists())
            .andExpect(MockMvcResultMatchers.jsonPath("$.aiBotToken").exists());
    }

    @Test
    public void testRegisterWithDuplicateUsername() throws Exception {
        // 测试重复用户名注册
        UserRegisterReq registerReq = new UserRegisterReq();
        registerReq.setUsername("admin"); // 使用已存在的用户名
        registerReq.setNickname("重复用户");

        String password = "Test123456";
        String encryptedPassword = SecureUtils.encryptByRsaPublicKey(password);
        registerReq.setPassword(encryptedPassword);
        registerReq.setConfirmPassword(encryptedPassword);

        registerReq.setEmail("duplicate@example.com");
        registerReq.setGender(GenderEnum.MALE);
        registerReq.setCaptcha("1234");
        registerReq.setUuid(IdUtil.fastUUID());

        // 执行注册请求，期望失败
        mockMvc.perform(MockMvcRequestBuilders.post("/auth/register")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JSONUtil.toJsonStr(registerReq))).andExpect(MockMvcResultMatchers.status().is4xxClientError());
    }

    @Test
    public void testRegisterWithInvalidPassword() throws Exception {
        // 测试密码不一致的情况
        UserRegisterReq registerReq = new UserRegisterReq();
        registerReq.setUsername("testuser" + System.currentTimeMillis());
        registerReq.setNickname("测试用户");

        String password1 = "Test123456";
        String password2 = "Test654321";
        registerReq.setPassword(SecureUtils.encryptByRsaPublicKey(password1));
        registerReq.setConfirmPassword(SecureUtils.encryptByRsaPublicKey(password2));

        registerReq.setEmail("test" + System.currentTimeMillis() + "@example.com");
        registerReq.setGender(GenderEnum.MALE);
        registerReq.setCaptcha("1234");
        registerReq.setUuid(IdUtil.fastUUID());

        // 执行注册请求，期望失败
        mockMvc.perform(MockMvcRequestBuilders.post("/auth/register")
            .contentType(MediaType.APPLICATION_JSON)
            .content(JSONUtil.toJsonStr(registerReq))).andExpect(MockMvcResultMatchers.status().is4xxClientError());
    }
}
