package com.fs.app.controller;

import com.fs.aiChat.domain.InterestAiChatMsg;
import com.fs.aiChat.domain.InterestAiSession;
import com.fs.aiChat.domain.SessionRoleInfo;
import com.fs.common.core.domain.R;
import com.fs.common.utils.StringUtils;
import com.fs.his.domain.FsInterestAiSession;
import com.fs.his.service.IHisInterestAiChatSessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(value = "/app/fsInterestAi")
@Api(tags = "兴趣AI")
public class InterestAiController extends AppBaseController {

    @Autowired
    private IHisInterestAiChatSessionService interestAiChatSessionService;

    /**
     * 创建会话
     *
     * @param fsInterestAiSession
     * @return
     */
    @PostMapping("/createSession")
    @ApiOperation(value = "创建会话")
    public R createSession(@RequestBody FsInterestAiSession fsInterestAiSession) {
        FsInterestAiSession session = interestAiChatSessionService.createSession(fsInterestAiSession);
        return R.ok().put("data", session);
    }

    /**
     * 根据当前用户获取用户与所有AI角色的会话列表
     *
     * @param session
     * @return
     */
    @GetMapping(value = "/getAllRolesListByUserId")
    @ApiOperation(value = "根据当前用户获取用户的会话列表")
    public R getAllRolesListByUserId(InterestAiSession session) {
        String userId = getUserId();
        log.info("拿到userId" + userId);
        if (null != session && session.getUserId() == null) {
            session.setUserId(Long.valueOf(userId));
        }
        startPage();
        List<InterestAiSession> list = interestAiChatSessionService.getAllRolesListByUserId(session);
        return R.ok().put("data", list);
    }

    /**
     * 根据会话id获取会话的详细信息
     *
     * @param sessionId
     * @return
     */
    @GetMapping(value = "/getSessionDetailInfoBySessionIdAndRoleId/{sessionId}/{roleId}")
    @ApiOperation(value = "根据会话id获取会话的详细信息")
    public R getSessionDetailInfoBySessionIdAndRoleId(@PathVariable(name = "sessionId") String sessionId,
                                                      @PathVariable(name = "roleId") Long roleId) {


        List<InterestAiChatMsg> chatList = new ArrayList<>();
        SessionRoleInfo roleInfo = null;
        if (null != roleId) {
            roleInfo = interestAiChatSessionService.getSessionRoleInfoByRoleId(roleId);
        }

        if (StringUtils.isNotBlank(sessionId)) {
            chatList = interestAiChatSessionService.getSessionChatList(sessionId);
        }
        return R.ok().put("roleInfo", roleInfo).put("chatList", chatList);
    }

    /**
     * 获取推荐角色列表
     *
     * @return
     */
    @GetMapping(value = "/getRecommendRoleList")
    @ApiOperation(value = "获取推荐角色列表")
    public R getRecommendRoleList() {
        String userId = getUserId();
        List<SessionRoleInfo> roleList = interestAiChatSessionService.getRecommendRoleList(userId);
        return R.ok().put("list", roleList);
    }

}
