package tech.easyflow.admin.controller.ai;

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.admin.dto.chatworkspace.ChatWorkspaceConversationView;
import tech.easyflow.admin.dto.chatworkspace.ChatWorkspaceSessionDetailView;
import tech.easyflow.admin.dto.chatworkspace.ChatWorkspaceSessionPage;
import tech.easyflow.admin.service.ai.ChatWorkspaceService;
import tech.easyflow.chatlog.domain.dto.ChatHistoryPage;
import tech.easyflow.chatlog.domain.dto.ChatMessageRecord;
import tech.easyflow.chatlog.domain.query.ChatPageQuery;
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 java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * 管理端聊天工作台控制器。
 */
@RestController
@RequestMapping("/api/v1/chatWorkspace")
public class ChatWorkspaceController {

    private final ChatWorkspaceService chatWorkspaceService;

    public ChatWorkspaceController(ChatWorkspaceService chatWorkspaceService) {
        this.chatWorkspaceService = chatWorkspaceService;
    }

    @GetMapping("/sessions")
    public Result<ChatWorkspaceSessionPage> listSessions(BigInteger assistantId, ChatPageQuery query) {
        return Result.ok(chatWorkspaceService.queryCurrentUserSessions(currentAccount(), assistantId, query));
    }

    @GetMapping("/sessions/{sessionId}")
    public Result<ChatWorkspaceSessionDetailView> getSession(@PathVariable BigInteger sessionId) {
        return Result.ok(chatWorkspaceService.getCurrentUserSession(currentAccount(), sessionId));
    }

    @GetMapping("/sessions/{sessionId}/messages")
    public Result<ChatHistoryPage> queryMessages(@PathVariable BigInteger sessionId, ChatPageQuery query) {
        return Result.ok(chatWorkspaceService.queryCurrentUserMessages(currentAccount(), sessionId, query));
    }

    @GetMapping("/sessions/{sessionId}/conversation")
    public Result<ChatWorkspaceConversationView> getConversation(@PathVariable BigInteger sessionId) {
        return Result.ok(chatWorkspaceService.getCurrentUserConversation(currentAccount(), sessionId));
    }

    @PostMapping("/sessions/{sessionId}/rename")
    public Result<Void> renameSession(@PathVariable BigInteger sessionId,
                                      @JsonBody(value = "title", required = true) String title) {
        chatWorkspaceService.renameCurrentUserSession(currentAccount(), sessionId, title);
        return Result.ok();
    }

    @PostMapping("/sessions/{sessionId}/delete")
    public Result<Void> deleteSession(@PathVariable BigInteger sessionId) {
        chatWorkspaceService.deleteCurrentUserSession(currentAccount(), sessionId);
        return Result.ok();
    }

    @GetMapping("/sessions/{sessionId}/rounds/{roundId}/variants")
    public Result<List<ChatMessageRecord>> listRoundVariants(@PathVariable BigInteger sessionId,
                                                             @PathVariable BigInteger roundId) {
        return Result.ok(chatWorkspaceService.listCurrentUserRoundVariants(currentAccount(), sessionId, roundId));
    }

    @PostMapping("/sessions/{sessionId}/rounds/{roundId}/selectVariant")
    public Result<ChatMessageRecord> selectRoundVariant(@PathVariable BigInteger sessionId,
                                                        @PathVariable BigInteger roundId,
                                                        @JsonBody(value = "variantIndex", required = true) Integer variantIndex) {
        return Result.ok(chatWorkspaceService.selectCurrentUserRoundVariant(currentAccount(), sessionId, roundId, variantIndex));
    }

    private LoginAccount currentAccount() {
        return SaTokenUtil.getLoginAccount();
    }
}
