package com.njcn.rdms.module.system.api.dept;

import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.test.core.ut.BaseMockitoUnitTest;
import com.njcn.rdms.module.system.service.dept.DeptService;
import com.njcn.rdms.module.system.service.dept.OrgLeaderRelationService;
import com.njcn.rdms.module.system.service.user.AdminUserService;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

class OrgLeaderApiImplTest extends BaseMockitoUnitTest {

    @Mock
    private OrgLeaderRelationService orgLeaderRelationService;

    @Mock
    private DeptService deptService;

    @Mock
    private AdminUserService adminUserService;

    @InjectMocks
    private OrgLeaderApiImpl orgLeaderApi;

    /** 用户没有任何生效的负责人关系，直接返回空集 */
    @Test
    void getReachableUserIds_returnsEmpty_whenUserIsNotLeader() {
        Long currentUserId = 1L;
        when(orgLeaderRelationService.listEffectiveDeptIdsByUserId(currentUserId))
                .thenReturn(Collections.emptySet());

        CommonResult<Set<Long>> result = orgLeaderApi.getReachableUserIds(currentUserId);

        assertTrue(result.getCheckedData().isEmpty());
    }

    /** 叶子节点 leader：只有直属部门用户，无子孙部门 */
    @Test
    void getReachableUserIds_returnsDirectSubordinates_whenUserIsLeafLeader() {
        Long currentUserId = 10L;
        Long deptId = 100L;
        Long subordinateId = 20L;

        when(orgLeaderRelationService.listEffectiveDeptIdsByUserId(currentUserId))
                .thenReturn(Set.of(deptId));
        // 叶子节点：listDescendantDeptIds 仅返回自身
        when(deptService.listDescendantDeptIds(Set.of(deptId)))
                .thenReturn(Set.of(deptId));
        when(adminUserService.listEnabledUserIdsByDeptIds(Set.of(deptId)))
                .thenReturn(new HashSet<>(Set.of(subordinateId)));

        CommonResult<Set<Long>> result = orgLeaderApi.getReachableUserIds(currentUserId);

        assertEquals(Set.of(subordinateId), result.getCheckedData());
    }

    /** 父节点 leader：结果包含所有子孙部门的可用用户 */
    @Test
    void getReachableUserIds_returnsRecursiveSubordinates_whenUserIsParentLeader() {
        Long currentUserId = 10L;
        Long rootDeptId = 100L;
        Long childDeptId = 200L;
        Long user1 = 21L;
        Long user2 = 22L;

        when(orgLeaderRelationService.listEffectiveDeptIdsByUserId(currentUserId))
                .thenReturn(Set.of(rootDeptId));
        // 父节点：listDescendantDeptIds 返回自身 + 子孙
        when(deptService.listDescendantDeptIds(Set.of(rootDeptId)))
                .thenReturn(Set.of(rootDeptId, childDeptId));
        when(adminUserService.listEnabledUserIdsByDeptIds(Set.of(rootDeptId, childDeptId)))
                .thenReturn(new HashSet<>(Set.of(user1, user2)));

        CommonResult<Set<Long>> result = orgLeaderApi.getReachableUserIds(currentUserId);

        assertEquals(Set.of(user1, user2), result.getCheckedData());
    }

    /** 自己（currentUserId）即使在部门用户列表中，也不应出现在结果集里 */
    @Test
    void getReachableUserIds_excludesSelf() {
        Long currentUserId = 10L;
        Long deptId = 100L;
        Long otherId = 20L;

        when(orgLeaderRelationService.listEffectiveDeptIdsByUserId(currentUserId))
                .thenReturn(Set.of(deptId));
        when(deptService.listDescendantDeptIds(Set.of(deptId)))
                .thenReturn(Set.of(deptId));
        // 返回集合包含 leader 自己
        when(adminUserService.listEnabledUserIdsByDeptIds(Set.of(deptId)))
                .thenReturn(new HashSet<>(Set.of(currentUserId, otherId)));

        CommonResult<Set<Long>> result = orgLeaderApi.getReachableUserIds(currentUserId);

        assertFalse(result.getCheckedData().contains(currentUserId), "结果集不能包含 currentUserId 自身");
        assertTrue(result.getCheckedData().contains(otherId));
    }

}
