package com.sonic.frog.controller.web;

import com.sonic.common.auth.domains.Session;
import com.sonic.common.rpc.Result;
import com.sonic.common.utils.RedisLock;
import com.sonic.frog.domain.input.*;
import com.sonic.frog.domain.output.AiUserInfoOutput;
import com.sonic.frog.domain.output.CreateEditAiUserOutput;
import com.sonic.frog.domain.output.ViewUnlockAlbumImgOutput;
import com.sonic.frog.service.AiUserSetService;
import com.sonic.frog.service.PayService;
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.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Web-业务支付
 *
 * @author: mzc
 * @date: 2025-07-11 11:43
 **/
@RestController
@Slf4j
public class PayWeb {

    @Autowired
    private PayService payService;
    @Autowired
    private RedisLock.RedisWrapper redisWrapper;
    @Autowired
    private RedisKeyUtils redisKeyUtils;

    @ApiOperation(value = "创建Ai形象-购买创作次数", tags = {"Web-业务支付"})
    @PostMapping("/web/ai/buy-create-image-count")
    public Result<Void> buyCreateImageCount(@Valid @RequestBody BuyCreateImageCountInput input, @ApiParam(hidden = true) Session session) {
        //redis键
        String buyCreateImageCountLockKey = redisKeyUtils.buyCreateImageCountLockKey(session.getUserId());
        //加锁处理
        RedisLock redisLock = new RedisLock(buyCreateImageCountLockKey, redisWrapper);
        redisLock.tryAcquireRun(() -> {
            payService.buyCreateImageCount(input, session.getUserId());
            return true;
        });
        return Result.success();
    }

    @ApiOperation(value = "解锁爱慕者", tags = {"Web-业务支付"})
    @PostMapping("/web/ai/unlock-like-you")
    public Result<ViewUnlockAlbumImgOutput> unlockLikeYou(@Valid @RequestBody UnlockLikeYouInput input, @ApiParam(hidden = true) Session session) {
        //redis键
        String unlockLikeYouLockKey = redisKeyUtils.unlockLikeYouLockKey(session.getUserId());
        //加锁处理
        RedisLock redisLock = new RedisLock(unlockLikeYouLockKey, redisWrapper);
        ViewUnlockAlbumImgOutput output =  redisLock.tryAcquireRun(() -> {
            return payService.unlockLikeYou(input, session.getUserId());
        });
        return Result.success(output);
    }

}
