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.chatlog.domain.dto.ChatMessageRecord;
import tech.easyflow.chatlog.domain.dto.PublicChatSessionRestoreResult;
import tech.easyflow.chatlog.service.PublicChatSessionRestoreService;
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("/api/v1/public-chat")
public class PublicChatSessionController {

    private final PublicChatSessionRestoreService publicChatSessionRestoreService;

    public PublicChatSessionController(PublicChatSessionRestoreService publicChatSessionRestoreService) {
        this.publicChatSessionRestoreService = publicChatSessionRestoreService;
    }

    @GetMapping("/session/restore")
    public Result<PublicChatSessionRestoreResult> restoreSession(BigInteger botId,
                                                                 BigInteger conversationId,
                                                                 Integer limit) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        BigInteger userId = account == null ? null : account.getId();
        PublicChatSessionRestoreResult result = publicChatSessionRestoreService.restoreSession(
                userId,
                botId,
                conversationId,
                limit
        );
        return Result.ok(result);
    }

    @GetMapping("/session/{sessionId}/rounds/{roundId}/variants")
    public Result<List<ChatMessageRecord>> listRoundVariants(@PathVariable BigInteger sessionId,
                                                             @PathVariable BigInteger roundId,
                                                             BigInteger botId) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        BigInteger userId = account == null ? null : account.getId();
        return Result.ok(publicChatSessionRestoreService.listVariants(userId, botId, sessionId, roundId));
    }

    @PostMapping("/session/{sessionId}/rounds/{roundId}/selectVariant")
    public Result<ChatMessageRecord> selectRoundVariant(@PathVariable BigInteger sessionId,
                                                        @PathVariable BigInteger roundId,
                                                        BigInteger botId,
                                                        @JsonBody(value = "variantIndex", required = true) Integer variantIndex) {
        LoginAccount account = SaTokenUtil.getLoginAccount();
        BigInteger userId = account == null ? null : account.getId();
        BigInteger operatorId = account == null ? BigInteger.ZERO : account.getId();
        return Result.ok(publicChatSessionRestoreService.selectVariant(userId, botId, sessionId, roundId, variantIndex, operatorId));
    }
}
