package com.sino.mci.test;

import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import java.util.UUID;

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;

public class AuthTestHelper {

    public static final String ADMIN_USERNAME = "admin";
    public static final String ADMIN_PASSWORD = "admin123";

    public static String createTenantAndLogin(MockMvc mockMvc) throws Exception {
        String tenantCode = "test-" + UUID.randomUUID().toString().substring(0, 8);
        return createTenantAndLogin(mockMvc, tenantCode, "测试租户");
    }

    public static String createTenantAndLogin(MockMvc mockMvc, String tenantCode, String tenantName) throws Exception {
        createTenant(mockMvc, tenantCode, tenantName);
        createAdminUser(mockMvc, tenantCode);
        return login(mockMvc, tenantCode);
    }

    public static void createTenant(MockMvc mockMvc, String tenantCode, String tenantName) throws Exception {
        String payload = String.format("{\"tenantCode\":\"%s\",\"tenantName\":\"%s\"}", tenantCode, tenantName);
        mockMvc.perform(post("/msg-platform/api/v1/account/tenant")
                        .contextPath("/msg-platform")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(payload))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(0));
    }

    public static void createAdminUser(MockMvc mockMvc, String tenantCode) throws Exception {
        String payload = String.format(
                "{\"username\":\"%s\",\"password\":\"%s\",\"realName\":\"管理员\",\"phone\":\"13800138000\",\"roleCodes\":[\"platform_admin\"]}",
                ADMIN_USERNAME, ADMIN_PASSWORD);
        mockMvc.perform(post("/msg-platform/api/v1/account/user")
                        .contextPath("/msg-platform")
                        .header("X-Tenant-Code", tenantCode)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(payload))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(0));
    }

    public static String login(MockMvc mockMvc, String tenantCode) throws Exception {
        String payload = String.format("{\"username\":\"%s\",\"password\":\"%s\"}", ADMIN_USERNAME, ADMIN_PASSWORD);
        MvcResult result = mockMvc.perform(post("/msg-platform/api/v1/account/login")
                        .contextPath("/msg-platform")
                        .header("X-Tenant-Code", tenantCode)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(payload))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.code").value(0))
                .andReturn();
        return "Bearer " + com.jayway.jsonpath.JsonPath.read(result.getResponse().getContentAsString(), "$.data.token");
    }
}
