package cn.hztech.sdk.server;

import cn.hztech.sdk.api.DepartmentApiClient;
import cn.hztech.sdk.integration.BaseIntegrationTest;
import cn.hztech.sdk.model.PageResult;
import cn.hztech.sdk.model.entity.Department;
import cn.hztech.sdk.model.request.DepartmentListRequest;
import cn.hztech.sdk.model.request.DepartmentSearchRequest;
import cn.hztech.sdk.model.request.DepartmentTreeRequest;
import org.junit.jupiter.api.*;

import java.util.List;

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

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

    private DepartmentApiClient departmentApiClient;

    @BeforeEach
    void setUp() {
        departmentApiClient = new DepartmentApiClient(httpClient);
    }

    @Test
    @Order(1)
    @DisplayName("测试查询部门详情 - 成功场景")
    void testGetDetail_Success() {
        System.out.println("\n=== 测试查询部门详情 ===");

        try {
            // Act - 执行API调用
            Department dept = departmentApiClient.getDetail("DEPT_001");

            // Assert - 验证结果
            assertNotNull(dept, "部门不应为空");
            assertNotNull(dept.getId(), "部门ID不应为空");
            assertEquals("DEPT_001", dept.getId(), "部门ID应匹配");
            assertNotNull(dept.getDeptName(), "部门名称不应为空");

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

    @Test
    @Order(2)
    @DisplayName("测试查询部门列表 - 成功场景")
    void testGetList_Success() {
        System.out.println("\n=== 测试查询部门列表 ===");

        try {
            // Arrange - 准备测试数据
            DepartmentListRequest request = new DepartmentListRequest();
            request.setPageNum(1);
            request.setPageSize(10);

            // Act - 执行API调用
            PageResult<Department> pageResult = departmentApiClient.getList(request);

            // Assert - 验证结果
            assertNotNull(pageResult, "分页结果不应为空");
            assertNotNull(pageResult.getList(), "部门列表不应为空");
            assertTrue(pageResult.getList().size() > 0, "部门列表应该有数据");
            assertNotNull(pageResult.getTotal(), "总数不应为空");

            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 testGetTree_Success() {
        System.out.println("\n=== 测试查询部门树 ===");

        try {
            DepartmentTreeRequest request = new DepartmentTreeRequest();
            request.setStatus(1);
            // Act - 执行API调用
            List<Department> tree = departmentApiClient.getTree(request);

            // Assert - 验证结果
            assertNotNull(tree, "部门树不应为空");
            assertTrue(tree.size() > 0, "部门树应该有数据");

            // 验证树结构
            Department rootDept = tree.get(0);
            assertNotNull(rootDept.getId(), "根部门ID不应为空");
            assertNotNull(rootDept.getDeptName(), "根部门名称不应为空");

            System.out.println("✓ 部门树查询成功");
            System.out.println("  根部门数: " + tree.size());
            System.out.println("  根部门: " + rootDept.getDeptName());
            if (rootDept.getChildren() != null && !rootDept.getChildren().isEmpty()) {
                System.out.println("  子部门数: " + rootDept.getChildren().size());
            }
        } catch (Exception e) {
            System.err.println("✗ 部门树查询失败: " + e.getMessage());
            e.printStackTrace();
            fail("部门树查询失败: " + e.getMessage());
        }
    }

    @Test
    @Order(4)
    @DisplayName("测试搜索部门 - 成功场景")
    void testSearch_Success() {
        System.out.println("\n=== 测试搜索部门 ===");

        try {
            // Arrange - 准备测试数据
            DepartmentSearchRequest request = new DepartmentSearchRequest();
            request.setKeyword("技术");
            request.setPageNum(1);
            request.setPageSize(10);

            // Act - 执行API调用
            PageResult<Department> pageResult = departmentApiClient.search(request);

            // Assert - 验证结果
            assertNotNull(pageResult, "分页结果不应为空");
            assertNotNull(pageResult.getList(), "搜索结果不应为空");

            System.out.println("✓ 部门搜索成功");
            System.out.println("  搜索结果数: " + pageResult.getList().size());
            System.out.println("  总数: " + pageResult.getTotal());

            if (!pageResult.getList().isEmpty()) {
                Department firstDept = pageResult.getList().get(0);
                System.out.println("  第一个部门: " + firstDept.getDeptName());
            }
        } catch (Exception e) {
            System.err.println("✗ 部门搜索失败: " + e.getMessage());
            e.printStackTrace();
            fail("部门搜索失败: " + e.getMessage());
        }
    }
}
