package cn.hztech.sdk.server;

import cn.hztech.sdk.api.MessageApiClient;
import cn.hztech.sdk.integration.BaseIntegrationTest;
import cn.hztech.sdk.model.entity.MessageBatchSendResult;
import cn.hztech.sdk.model.entity.MessageSendResult;
import cn.hztech.sdk.model.request.MessageBatchSendRequest;
import cn.hztech.sdk.model.request.MessageSendRequest;
import org.junit.jupiter.api.*;

import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Message API服务端集成测试
 * <p>
 * 测试消息发送相关的所有API接口
 * <p>
 * 前置条件：
 * <ul>
 *   <li>platform-server已启动（端口8080）</li>
 *   <li>使用test profile启动</li>
 *   <li>测试数据已加载（USER_0001等）</li>
 * </ul>
 * <p>
 * 运行方式：
 * <pre>
 * # 启动服务器
 * cd sdk/platform-server
 * mvn spring-boot:run -Dspring.profiles.active=test
 *
 * # 运行测试
 * mvn test -Dtest=MessageApiClientServerIntegrationTest
 * </pre>
 *
 * @author SDK Team
 * @since 1.0.0
 */
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class MessageApiClientServerIntegrationTest extends BaseIntegrationTest {

    private MessageApiClient messageApiClient;

    @BeforeEach
    void setUp() {
        messageApiClient = new MessageApiClient(httpClient);
    }

    @Test
    @Order(1)
    @DisplayName("测试发送单条消息 - 成功场景")
    void testSend_Success() {
        System.out.println("\n=== 测试发送单条消息 ===");

        try {
            // Arrange - 准备测试数据
            MessageSendRequest request = new MessageSendRequest();
            request.setReceiverId("USER_0001");
//            request.setReceiverType("USER");
            request.setTitle("测试消息");
            request.setContent("这是一条测试消息内容");
            request.setMessageType("NOTICE");

            // Act - 执行API调用
            MessageSendResult result = messageApiClient.send(request);

            // Assert - 验证结果
            assertNotNull(result, "发送结果不应为空");

            System.out.println("✓ 消息发送成功");
            System.out.println("  接收者ID: " + request.getReceiverId());
//            System.out.println("  接收者类型: " + request.getReceiverType());
            System.out.println("  消息类型: " + request.getMessageType());
        } catch (Exception e) {
            System.err.println("✗ 消息发送失败: " + e.getMessage());
            e.printStackTrace();
            fail("消息发送失败: " + e.getMessage());
        }
    }

    @Test
    @Order(2)
    @DisplayName("测试批量发送消息 - 成功场景")
    void testBatchSend_Success() {
        System.out.println("\n=== 测试批量发送消息 ===");

        try {
            // Arrange - 准备测试数据
            MessageBatchSendRequest request = new MessageBatchSendRequest();
            request.setReceiverIds(Arrays.asList("USER_0001", "USER_0002"));
//            request.setReceiverType("USER");
            request.setTitle("批量测试消息");
            request.setContent("这是一条批量测试消息内容");
            request.setMessageType("NOTICE");

            // Act - 执行API调用
            MessageBatchSendResult result = messageApiClient.batchSend(request);

            // Assert - 验证结果
            assertNotNull(result, "发送结果不应为空");

            System.out.println("✓ 批量消息发送成功");
            System.out.println("  接收者数量: " + request.getReceiverIds().size());
            System.out.println("  接收者: " + request.getReceiverIds());
//            System.out.println("  接收者类型: " + request.getReceiverType());
            System.out.println("  消息类型: " + request.getMessageType());
        } catch (Exception e) {
            System.err.println("✗ 批量消息发送失败: " + e.getMessage());
            e.printStackTrace();
            fail("批量消息发送失败: " + e.getMessage());
        }
    }
}
