package cn.hztech.sdk.server;

import cn.hztech.sdk.api.PermissionApiClient;
import cn.hztech.sdk.integration.BaseIntegrationTest;
import cn.hztech.sdk.model.entity.Menu;
import org.junit.jupiter.api.*;

import java.util.List;

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

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

    private PermissionApiClient permissionApiClient;

    @BeforeEach
    void setUp() {
        permissionApiClient = new PermissionApiClient(httpClient);
    }

    @Test
    @Order(1)
    @DisplayName("测试查询用户菜单 - 成功场景")
    void testGetUserMenu_Success() {
        System.out.println("\n=== 测试查询用户菜单 ===");

        try {
            // Act - 执行API调用
            List<Menu> menuTree = permissionApiClient.getUserMenu("USER_0001");

            // Assert - 验证结果
            assertNotNull(menuTree, "用户菜单不应为空");

            System.out.println("✓ 用户菜单查询成功");
            System.out.println("  菜单数: " + menuTree.size());

            if (!menuTree.isEmpty()) {
                Menu firstMenu = menuTree.get(0);
//                System.out.println("  第一个菜单: " + firstMenu.getMenuName());
//                System.out.println("  菜单类型: " + firstMenu.getMenuType());
            }
        } catch (Exception e) {
            System.err.println("✗ 用户菜单查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("用户菜单查询失败: " + e.getMessage());
        }
    }

    @Test
    @Order(2)
    @DisplayName("测试查询角色菜单 - 成功场景")
    void testGetRoleMenu_Success() {
        System.out.println("\n=== 测试查询角色菜单 ===");

        try {
            // Act - 执行API调用
            List<Menu> menuTree = permissionApiClient.getRoleMenu("ROLE_001");

            // Assert - 验证结果
            assertNotNull(menuTree, "角色菜单不应为空");

            System.out.println("✓ 角色菜单查询成功");
            System.out.println("  菜单数: " + menuTree.size());

            if (!menuTree.isEmpty()) {
                Menu firstMenu = menuTree.get(0);
//                System.out.println("  第一个菜单: " + firstMenu.getMenuName());
//                System.out.println("  菜单类型: " + firstMenu.getMenuType());
            }
        } catch (Exception e) {
            System.err.println("✗ 角色菜单查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("角色菜单查询失败: " + e.getMessage());
        }
    }
}
