package tech.easyflow.usercenter.controller.ai;

import cn.dev33.satoken.annotation.SaIgnore;
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.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.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 java.math.BigInteger;
import java.util.List;

@RestController
@RequestMapping("/userCenter/chatHistory")
@SaIgnore
public class UcChatHistoryController {

    private final ChatHistoryManageService chatHistoryManageService;

    public UcChatHistoryController(ChatHistoryManageService chatHistoryManageService) {
        this.chatHistoryManageService = chatHistoryManageService;
    }

    @GetMapping("/sessions")
    public Result<ChatSessionPage> listSessions(BigInteger assistantId, ChatPageQuery query) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(chatHistoryManageService.queryUserSessions(account.getId(), assistantId, query));
    }

    @GetMapping("/sessions/{sessionId}")
    public Result<ChatSessionSummary> getSession(@PathVariable BigInteger sessionId) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(chatHistoryManageService.getUserSession(account.getId(), sessionId));
    }

    @GetMapping("/sessions/{sessionId}/messages")
    public Result<ChatHistoryPage> queryMessages(@PathVariable BigInteger sessionId, ChatPageQuery query) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(chatHistoryManageService.queryUserMessages(account.getId(), sessionId, query));
    }

    @PostMapping("/sessions/{sessionId}/rename")
    public Result<Void> renameSession(@PathVariable BigInteger sessionId,
                                      @JsonBody(value = "title", required = true) String title) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        chatHistoryManageService.renameUserSession(account.getId(), sessionId, title, account.getId());
        return Result.ok();
    }

    @PostMapping("/sessions/{sessionId}/delete")
    public Result<Void> deleteSession(@PathVariable BigInteger sessionId) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        chatHistoryManageService.deleteUserSession(account.getId(), sessionId, account.getId());
        return Result.ok();
    }

    @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.listUserRoundVariants(account.getId(), 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) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        return Result.ok(chatHistoryManageService.selectUserRoundVariant(account.getId(), sessionId, roundId, variantIndex, account.getId()));
    }
}
