package cn.hztech.sdk;

import cn.hztech.sdk.api.*;
import cn.hztech.sdk.integration.BaseIntegrationTest;
import cn.hztech.sdk.model.PageResult;
import cn.hztech.sdk.model.entity.*;
import cn.hztech.sdk.model.request.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;

import java.util.List;

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

/**
 * SDK与模拟服务器集成测试
 * <p>
 * 测试说明：
 * 1. 运行此测试前，请确保platform-server服务器已启动（端口：8081）
 * 2. 使用 @Order 注解控制测试执行顺序
 * 3. 测试失败时会打印详细的错误信息
 */
@TestMethodOrder(OrderAnnotation.class)
class ServerIntegrationTest extends BaseIntegrationTest {

    private UserApiClient userApiClient;
    private DepartmentApiClient departmentApiClient;
    private RoleApiClient roleApiClient;
    private MenuApiClient menuApiClient;
    private PermissionApiClient permissionApiClient;

    @BeforeEach
    void setUp() {
        // 初始化所有API客户端
        userApiClient = new UserApiClient(httpClient);
        departmentApiClient = new DepartmentApiClient(httpClient);
        roleApiClient = new RoleApiClient(httpClient);
        menuApiClient = new MenuApiClient(httpClient);
        permissionApiClient = new PermissionApiClient(httpClient);
    }

    // ==================== 用户接口测试 ====================

    @Test
    @Order(1)
    @DisplayName("测试查询用户详情")
    void testGetUserDetail() {
        System.out.println("\n=== 测试查询用户详情 ===");

        try {
            User user = userApiClient.getDetail("USER_0001");
            assertNotNull(user, "用户不应为空");
            assertNotNull(user.getId(), "用户ID不应为空");
            assertNotNull(user.getAccount(), "用户账号不应为空");

            System.out.println("✓ 用户详情查询成功");
            System.out.println("  用户ID: " + user.getId());
            System.out.println("  账号: " + user.getAccount());
            System.out.println("  姓名: " + user.getRealName());
        } catch (Exception e) {
            System.err.println("✗ 用户详情查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("用户详情查询失败: " + e.getMessage());
        }
    }

    @Test
    @Order(2)
    @DisplayName("测试查询用户列表")
    void testGetUserList() {
        System.out.println("\n=== 测试查询用户列表 ===");

        try {
            UserListRequest request = new UserListRequest();
            request.setPageNum(1);
            request.setPageSize(10);

            PageResult<User> pageResult = userApiClient.getList(request);
            assertNotNull(pageResult, "分页结果不应为空");
            assertNotNull(pageResult.getList(), "用户列表不应为空");
            assertTrue(pageResult.getList().size() > 0, "用户列表应该有数据");

            System.out.println("✓ 用户列表查询成功");
            System.out.println("  总数: " + pageResult.getTotal());
            System.out.println("  当前页: " + pageResult.getPageNum());
            System.out.println("  页大小: " + pageResult.getPageSize());
            System.out.println("  用户数: " + pageResult.getList().size());
        } catch (Exception e) {
            System.err.println("✗ 用户列表查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("用户列表查询失败: " + e.getMessage());
        }
    }

    @Test
    @Order(3)
    @DisplayName("测试搜索用户")
    void testSearchUser() {
        System.out.println("\n=== 测试搜索用户 ===");

        try {
            UserSearchRequest request = new UserSearchRequest();
            request.setKeyword("USER");
            request.setPageNum(1);
            request.setPageSize(10);

            PageResult<User> pageResult = userApiClient.search(request);
            assertNotNull(pageResult, "分页结果不应为空");
            assertNotNull(pageResult.getList(), "搜索结果不应为空");

            System.out.println("✓ 用户搜索成功");
            System.out.println("  搜索结果数: " + pageResult.getList().size());
        } catch (Exception e) {
            System.err.println("✗ 用户搜索失败: " + e.getMessage());
            e.printStackTrace();
            fail("用户搜索失败: " + e.getMessage());
        }
    }

    @Test
    @Order(4)
    @DisplayName("测试查询用户角色")
    void testGetUserRoles() {
        System.out.println("\n=== 测试查询用户角色 ===");

        try {
            Role user = userApiClient.getRoles("USER_0001");
            assertNotNull(user, "用户不应为空");
            assertNotNull(user.getId(), "角色ID不应为空");
            assertNotNull(user.getRoleName(), "角色名称不应为空");

            System.out.println("✓ 用户角色查询成功");
            System.out.println("  角色ID: " + user.getId());
            System.out.println("  角色名称: " + user.getRoleName());
        } catch (Exception e) {
            System.err.println("✗ 用户角色查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("用户角色查询失败: " + e.getMessage());
        }
    }

    @Test
    @Order(5)
    @DisplayName("测试查询用户部门")
    void testGetUserDepartment() {
        System.out.println("\n=== 测试查询用户部门 ===");

        try {
            Department user = userApiClient.getDepartment("USER_0001");
            assertNotNull(user, "用户不应为空");
            assertNotNull(user.getId(), "部门ID不应为空");
            assertNotNull(user.getDeptName(), "部门名称不应为空");

            System.out.println("✓ 用户部门查询成功");
            System.out.println("  部门ID: " + user.getId());
            System.out.println("  部门名称: " + user.getDeptName());
        } catch (Exception e) {
            System.err.println("✗ 用户部门查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("用户部门查询失败: " + e.getMessage());
        }
    }

    // ==================== 部门接口测试 ====================

    @Test
    @Order(6)
    @DisplayName("测试查询部门详情")
    void testGetDepartmentDetail() {
        System.out.println("\n=== 测试查询部门详情 ===");

        try {
            Department dept = departmentApiClient.getDetail("DEPT_001");
            assertNotNull(dept, "部门不应为空");
            assertNotNull(dept.getId(), "部门ID不应为空");
            assertNotNull(dept.getDeptName(), "部门名称不应为空");

            System.out.println("✓ 部门详情查询成功");
            System.out.println("  部门ID: " + dept.getId());
            System.out.println("  部门名称: " + dept.getDeptName());
        } catch (Exception e) {
            System.err.println("✗ 部门详情查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("部门详情查询失败: " + e.getMessage());
        }
    }

    @Test
    @Order(7)
    @DisplayName("测试查询部门列表")
    void testGetDepartmentList() {
        System.out.println("\n=== 测试查询部门列表 ===");

        try {
            DepartmentListRequest request = new DepartmentListRequest();
            request.setPageNum(1);
            request.setPageSize(10);

            PageResult<Department> pageResult = departmentApiClient.getList(request);
            assertNotNull(pageResult, "分页结果不应为空");
            assertNotNull(pageResult.getList(), "部门列表不应为空");

            System.out.println("✓ 部门列表查询成功");
            System.out.println("  部门数: " + pageResult.getList().size());
        } catch (Exception e) {
            System.err.println("✗ 部门列表查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("部门列表查询失败: " + e.getMessage());
        }
    }

    @Test
    @Order(8)
    @DisplayName("测试查询部门树")
    void testGetDepartmentTree() {
        System.out.println("\n=== 测试查询部门树 ===");

        try {
            DepartmentTreeRequest request = new DepartmentTreeRequest();
            request.setStatus(1);
            List<Department> tree = departmentApiClient.getTree(request);
            assertNotNull(tree, "部门树不应为空");
            assertTrue(tree.size() > 0, "部门树应该有数据");

            System.out.println("✓ 部门树查询成功");
            System.out.println("  根部门数: " + tree.size());
        } catch (Exception e) {
            System.err.println("✗ 部门树查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("部门树查询失败: " + e.getMessage());
        }
    }

    // ==================== 角色接口测试 ====================

    @Test
    @Order(9)
    @DisplayName("测试查询角色详情")
    void testGetRoleDetail() {
        System.out.println("\n=== 测试查询角色详情 ===");

        try {
            Role role = roleApiClient.getDetail("ROLE_001");
            assertNotNull(role, "角色不应为空");
            assertNotNull(role.getId(), "角色ID不应为空");
            assertNotNull(role.getRoleName(), "角色名称不应为空");

            System.out.println("✓ 角色详情查询成功");
            System.out.println("  角色ID: " + role.getId());
            System.out.println("  角色名称: " + role.getRoleName());
        } catch (Exception e) {
            System.err.println("✗ 角色详情查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("角色详情查询失败: " + e.getMessage());
        }
    }

    @Test
    @Order(10)
    @DisplayName("测试查询角色列表")
    void testGetRoleList() {
        System.out.println("\n=== 测试查询角色列表 ===");

        try {
            RoleListRequest request = new RoleListRequest();
            request.setPageNum(1);
            request.setPageSize(10);

            PageResult<Role> pageResult = roleApiClient.getList(request);
            assertNotNull(pageResult, "分页结果不应为空");
            assertNotNull(pageResult.getList(), "角色列表不应为空");

            System.out.println("✓ 角色列表查询成功");
            System.out.println("  角色数: " + pageResult.getList().size());
        } catch (Exception e) {
            System.err.println("✗ 角色列表查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("角色列表查询失败: " + e.getMessage());
        }
    }

    // ==================== 菜单接口测试 ====================

    @Test
    @Order(11)
    @DisplayName("测试查询菜单数据")
    void testQueryMenu() {
        System.out.println("\n=== 测试查询菜单数据 ===");

        try {
            MenuQueryRequest request = new MenuQueryRequest();

            List<Menu> menuTree = ( List<Menu>)menuApiClient.query(request);
            assertNotNull(menuTree, "菜单树不应为空");
            assertTrue(menuTree.size() > 0, "菜单树应该有数据");

            System.out.println("✓ 菜单查询成功");
            System.out.println("  根菜单数: " + menuTree.size());
        } catch (Exception e) {
            System.err.println("✗ 菜单查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("菜单查询失败: " + e.getMessage());
        }
    }

    // ==================== 权限接口测试 ====================

    @Test
    @Order(12)
    @DisplayName("测试查询用户菜单")
    void testGetUserMenu() {
        System.out.println("\n=== 测试查询用户菜单 ===");

        try {
            List<Menu> menuTree = permissionApiClient.getUserMenu("USER_0001");
            assertNotNull(menuTree, "用户菜单不应为空");

            System.out.println("✓ 用户菜单查询成功");
            System.out.println("  菜单数: " + menuTree.size());
        } catch (Exception e) {
            System.err.println("✗ 用户菜单查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("用户菜单查询失败: " + e.getMessage());
        }
    }

    @Test
    @Order(13)
    @DisplayName("测试查询角色菜单")
    void testGetRoleMenu() {
        System.out.println("\n=== 测试查询角色菜单 ===");

        try {
            List<Menu> menuTree = permissionApiClient.getRoleMenu("ROLE_001");
            assertNotNull(menuTree, "角色菜单不应为空");

            System.out.println("✓ 角色菜单查询成功");
            System.out.println("  菜单数: " + menuTree.size());
        } catch (Exception e) {
            System.err.println("✗ 角色菜单查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("角色菜单查询失败: " + e.getMessage());
        }
    }
}
