package tech.easyflow.admin.controller.ai;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.easyflow.agent.service.AgentOptionQueryService;
import tech.easyflow.agent.vo.AgentOptionView;
import tech.easyflow.chatlog.domain.dto.ChatHistoryPage;
import tech.easyflow.chatlog.domain.dto.ChatMessageRecord;
import tech.easyflow.chatlog.domain.dto.ChatSessionPage;
import tech.easyflow.chatlog.domain.dto.ChatSessionSummary;
import tech.easyflow.chatlog.domain.query.ChatPageQuery;
import tech.easyflow.chatlog.domain.query.ChatSessionFilterQuery;
import tech.easyflow.chatlog.service.ChatHistoryManageService;
import tech.easyflow.common.domain.Result;
import tech.easyflow.common.entity.LoginAccount;
import tech.easyflow.common.satoken.util.SaTokenUtil;
import tech.easyflow.common.web.jsonbody.JsonBody;
import tech.easyflow.system.service.CategoryPermissionService;

import java.math.BigInteger;
import java.util.List;

@RestController
@RequestMapping("/api/v1/chatHistory")
@SaCheckPermission("/api/v1/chatHistory/query")
public class ChatHistoryController {

    private final ChatHistoryManageService chatHistoryManageService;
    private final CategoryPermissionService categoryPermissionService;
    private final AgentOptionQueryService agentOptionQueryService;

    /**
     * 创建聊天历史控制器。
     *
     * @param chatHistoryManageService 聊天历史管理服务
     * @param categoryPermissionService 账号权限服务
     * @param agentOptionQueryService Agent 安全选项服务
     */
    public ChatHistoryController(ChatHistoryManageService chatHistoryManageService,
                                 CategoryPermissionService categoryPermissionService,
                                 AgentOptionQueryService agentOptionQueryService) {
        this.chatHistoryManageService = chatHistoryManageService;
        this.categoryPermissionService = categoryPermissionService;
        this.agentOptionQueryService = agentOptionQueryService;
    }

    /**
     * 查询聊天记录筛选可使用的 Agent。
     *
     * @return Agent 安全选项
     */
    @GetMapping("/agentOptions")
    public Result<List<AgentOptionView>> agentOptions() {
        return Result.ok(agentOptionQueryService.listAgentOptions(false));
    }

    /**
     * 分页查询当前账号可见的 Agent 会话。
     *
     * @param query 会话筛选条件
     * @return 会话分页结果
     */
    @GetMapping("/sessions")
    public Result<ChatSessionPage> listSessions(ChatSessionFilterQuery query) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(chatHistoryManageService.queryAdminSessions(
                account.getId(),
                categoryPermissionService.isSuperAdmin(account),
                query
        ));
    }

    /**
     * 获取当前账号可见的 Agent 会话详情。
     *
     * @param sessionId 会话 ID
     * @return 会话详情
     */
    @GetMapping("/sessions/{sessionId}")
    public Result<ChatSessionSummary> getSession(@PathVariable BigInteger sessionId) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(chatHistoryManageService.getAdminSession(
                account.getId(),
                categoryPermissionService.isSuperAdmin(account),
                sessionId
        ));
    }

    /**
     * 分页查询当前账号可见会话的消息。
     *
     * @param sessionId 会话 ID
     * @param query 消息分页条件
     * @return 消息分页结果
     */
    @GetMapping("/sessions/{sessionId}/messages")
    public Result<ChatHistoryPage> queryMessages(@PathVariable BigInteger sessionId, ChatPageQuery query) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(chatHistoryManageService.queryAdminMessages(
                account.getId(),
                categoryPermissionService.isSuperAdmin(account),
                sessionId,
                query
        ));
    }

    /**
     * 查询当前账号可见会话的答案版本。
     *
     * @param sessionId 会话 ID
     * @param roundId 对话轮次 ID
     * @return 答案版本列表
     */
    @GetMapping("/sessions/{sessionId}/rounds/{roundId}/variants")
    public Result<List<ChatMessageRecord>> listRoundVariants(@PathVariable BigInteger sessionId,
                                                             @PathVariable BigInteger roundId) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(chatHistoryManageService.listAdminRoundVariants(
                account.getId(),
                categoryPermissionService.isSuperAdmin(account),
                sessionId,
                roundId
        ));
    }

    /**
     * 选择当前账号可见会话的答案版本。
     *
     * @param sessionId 会话 ID
     * @param roundId 对话轮次 ID
     * @param variantIndex 目标版本索引
     * @return 选中的答案记录
     */
    @PostMapping("/sessions/{sessionId}/rounds/{roundId}/selectVariant")
    public Result<ChatMessageRecord> selectRoundVariant(@PathVariable BigInteger sessionId,
                                                        @PathVariable BigInteger roundId,
                                                        @JsonBody(value = "variantIndex", required = true) Integer variantIndex) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(chatHistoryManageService.selectAdminRoundVariant(
                account.getId(),
                categoryPermissionService.isSuperAdmin(account),
                sessionId,
                roundId,
                variantIndex
        ));
    }
}
