package com.sonic.frog.controller.web;

import com.sonic.common.auth.domains.Session;
import com.sonic.common.rpc.Page;
import com.sonic.common.rpc.Result;
import com.sonic.common.utils.RedisLock;
import com.sonic.frog.domain.input.*;
import com.sonic.frog.domain.output.*;
import com.sonic.frog.service.AiUserHeartbeatRankService;
import com.sonic.frog.service.AiUserHeartbeatRelationService;
import com.sonic.frog.utils.RedisKeyUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
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 code
 */
@RestController
@Slf4j
public class AiUserHeartbeatWeb {

    @Autowired
    private AiUserHeartbeatRelationService aiUserHeartbeatRelationService;
    @Autowired
    private RedisLock.RedisWrapper redisWrapper;
    @Autowired
    private RedisKeyUtils redisKeyUtils;
    @Autowired
    private AiUserHeartbeatRankService aiUserHeartbeatRankService;

    @ApiOperation(value = "关系列表", tags = {"Web-AI用户心动等级"})
    @PostMapping("/web/ai-user/heartbeat-relation-list")
    public Result<Page<HeartbeatRelationListOutput>> heartbeatRelationList(@Validated @RequestBody HeartbeatRelationListInput input, @ApiParam(hidden = true) Session session) {
        return Result.success(aiUserHeartbeatRelationService.heartbeatRelationList(session.getUserId(), input));
    }

    @ApiOperation(value = "展示关系开关", tags = {"Web-AI用户心动等级"})
    @PostMapping("/web/ai-user/heartbeat-relation-switch")
    public Result<Void> heartbeatRelationSwitch(@Validated @RequestBody HeartbeatRelationSwitchInput input, @ApiParam(hidden = true) Session session) {
        aiUserHeartbeatRelationService.heartbeatRelationSwitch(session.getUserId(), input);
        return Result.success();
    }

    @ApiOperation(value = "获取心动等级", tags = {"Web-AI用户心动等级"})
    @PostMapping("/web/ai-user/heartbeat-level")
    public Result<HeartbeatLevelOutput> getAiUserHeartbeatLevel(@Validated @RequestBody HeartbeatLevelInput input, @ApiParam(hidden = true) Session session) {
        return Result.success(aiUserHeartbeatRelationService.getAiUserHeartbeatLevel(session.getUserId(), input.getAiId()));
    }

    @ApiOperation(value = "购买已扣减的心动值", tags = {"Web-AI用户心动等级"})
    @PostMapping("/web/ai-user/buy-heartbeat-val")
    public Result<Void> buyHeartbeatVal(@Validated @RequestBody BuyHeartbeatValInput input, @ApiParam(hidden = true) Session session) {
        //redis键
        String buyHeartbeatValLockKey = redisKeyUtils.buyHeartbeatValLockKey(session.getUserId());
        //加锁处理
        RedisLock redisLock = new RedisLock(buyHeartbeatValLockKey, redisWrapper);
        redisLock.tryAcquireRun(() -> {
            aiUserHeartbeatRelationService.buyHeartbeatVal(session.getUserId(), input);
            return true;
        });
        return Result.success();
    }

    @ApiOperation(value = "24小时未聊天扣除心动值(登录后调一次)", tags = {"Web-AI用户心动等级"})
    @PostMapping("/web/ai-user/without-chat-subtract-heartbeat-val")
    public Result<Void> withoutChatSubtractHeartbeatVal(@ApiParam(hidden = true) Session session) {
//        aiUserHeartbeatRelationService.withoutChatSubtractHeartbeatVal(session.getUserId());
        return Result.success();
    }

    @ApiOperation(value = "获取当前用户的心动值排名", tags = {"Web-AI用户心动等级"})
    @PostMapping("/web/ai-user/heartbeat-rank")
    public Result<Void> heartbeatRank(@ApiParam(hidden = true) Session session) {
        return Result.success(aiUserHeartbeatRankService.getCurrentUserRank(session.getUserId()));
    }

}
