package tech.easyflow.admin.controller.ai;

import org.mockito.MockedStatic;
import org.testng.annotations.Test;
import tech.easyflow.chatlog.domain.dto.ChatSessionPage;
import tech.easyflow.chatlog.domain.dto.ChatSessionSummary;
import tech.easyflow.chatlog.domain.query.ChatSessionFilterQuery;
import tech.easyflow.chatlog.service.ChatHistoryManageService;
import tech.easyflow.agent.service.AgentOptionQueryService;
import tech.easyflow.common.entity.LoginAccount;
import tech.easyflow.common.satoken.util.SaTokenUtil;
import tech.easyflow.system.service.CategoryPermissionService;

import java.math.BigInteger;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * {@link ChatHistoryController} 数据范围测试。
 */
public class ChatHistoryControllerTest {

    /**
     * 验证普通账号查询时将本人范围传给服务层。
     */
    @Test
    public void listSessionsShouldUseCurrentUserScopeForRegularAccount() {
        BigInteger accountId = BigInteger.valueOf(20);
        ChatHistoryManageService service = mock(ChatHistoryManageService.class);
        CategoryPermissionService permissionService = mock(CategoryPermissionService.class);
        ChatHistoryController controller = new ChatHistoryController(
                service, permissionService, mock(AgentOptionQueryService.class));
        ChatSessionFilterQuery query = new ChatSessionFilterQuery();
        LoginAccount account = loginAccount(accountId);
        when(permissionService.isSuperAdmin(account)).thenReturn(false);
        when(service.queryAdminSessions(accountId, false, query)).thenReturn(new ChatSessionPage());

        try (MockedStatic<SaTokenUtil> saToken = mockStatic(SaTokenUtil.class)) {
            saToken.when(SaTokenUtil::getLoginAccount).thenReturn(account);

            controller.listSessions(query);
        }

        verify(service).queryAdminSessions(accountId, false, query);
    }

    /**
     * 验证超级管理员查询详情时保留全量范围。
     */
    @Test
    public void getSessionShouldUseAllScopeForSuperAdmin() {
        BigInteger accountId = BigInteger.ONE;
        BigInteger sessionId = BigInteger.valueOf(30);
        ChatHistoryManageService service = mock(ChatHistoryManageService.class);
        CategoryPermissionService permissionService = mock(CategoryPermissionService.class);
        ChatHistoryController controller = new ChatHistoryController(
                service, permissionService, mock(AgentOptionQueryService.class));
        LoginAccount account = loginAccount(accountId);
        when(permissionService.isSuperAdmin(account)).thenReturn(true);
        when(service.getAdminSession(accountId, true, sessionId)).thenReturn(new ChatSessionSummary());

        try (MockedStatic<SaTokenUtil> saToken = mockStatic(SaTokenUtil.class)) {
            saToken.when(SaTokenUtil::getLoginAccount).thenReturn(account);

            controller.getSession(sessionId);
        }

        verify(service).getAdminSession(accountId, true, sessionId);
    }

    /**
     * 构造登录账号。
     *
     * @param accountId 账号 ID
     * @return 登录账号
     */
    private LoginAccount loginAccount(BigInteger accountId) {
        LoginAccount account = new LoginAccount();
        account.setId(accountId);
        return account;
    }
}
