package com.sonic.pigeon.controller.web;

import com.sonic.common.auth.domains.Session;
import com.sonic.common.rpc.Result;
import com.sonic.common.utils.RedisLock;
import com.sonic.pigeon.domain.input.FeedbackInput;
import com.sonic.pigeon.service.AiChatFeedbackService;
import com.sonic.pigeon.utils.RedisKeyUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Web-AI聊天反馈
 * @author coder
 */
@Validated
@RestController
public class FeedbackWeb {

    @Autowired
    private AiChatFeedbackService aiChatFeedbackService;
    @Autowired
    private RedisLock.RedisWrapper redisWrapper;
    @Autowired
    private RedisKeyUtils redisKeyUtils;

    @ApiOperation(value = "点赞点踩反馈", tags = {"Web-AI聊天反馈"})
    @PostMapping("/web/fb/v1")
    public Result<Void> feedback(@RequestBody FeedbackInput input, @ApiParam(hidden = true) Session session) {
        //操作人加锁，防止并发操作
        RedisLock redisLock = new RedisLock(redisKeyUtils.feedbackLockKey(session.getUserId()), redisWrapper);
        redisLock.tryAcquireRun(() -> {
            aiChatFeedbackService.feedback(session.getUserId(), input);
            return true;
        });
        return Result.success();
    }

}
