package shop.alien.second.controller;

import com.alibaba.fastjson2.JSONObject;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.second.SecondUserCredit;
import shop.alien.entity.second.vo.SecondUserCreditVo;
import shop.alien.second.service.SecondUserCreditService;

/**
 * 二手平台用户积分控制器
 */
@Slf4j
@Api(tags = {"二期-用户积分"})
@CrossOrigin
@RestController
@RequestMapping("/userPoints")
@RequiredArgsConstructor
public class SecondUserCreditController {
    
    private final SecondUserCreditService secondUserCreditService;
    
    @ApiOperation("获取用户积分")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "path", required = true)
    })
    @GetMapping("/{userId}")
    public R<SecondUserCredit> getUserPoints(@PathVariable Integer userId) {
        log.info("获取用户积分，userId={}", userId);
        try {
            SecondUserCredit userPoints = secondUserCreditService.getByUserId(userId);
            return R.data(userPoints);
        } catch (Exception e) {
            log.error("获取用户积分失败，userId={}", userId, e);
            return R.fail("获取用户积分失败");
        }
    }
    
    @ApiOperation("更新用户积分")
    @ApiOperationSupport(order = 2)
    @PostMapping("/update/credit")
    public R<Boolean> updateUserPoints(SecondUserCreditVo credit) {
        log.info("更新用户积分，credit={}", credit);
        try {
            boolean result = secondUserCreditService.updatePoints(credit);
            if (result) {
                return R.success("积分更新成功");
            } else {
                return R.fail("积分更新失败");
            }
        } catch (Exception e) {
            log.error("更新用户积分失败，credit={}", credit, e);
            return R.fail("积分更新失败");
        }
    }
    
    @ApiOperation("新建用户积分记录")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "path", required = true),
        @ApiImplicitParam(name = "initialPoints", value = "初始积分值", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pointsType", value = "初始积分类型", dataType = "Integer", paramType = "query")
    })
    @PostMapping("/create")
    public void createPointsRecord(@RequestParam("userId")  Integer userId,
                                   @RequestParam("initialPoints")  Integer initialPoints,
                                   @RequestParam("pointsType")  Integer pointsType) {
        log.info("新建用户积分记录，userId={}, initialPoints={}", userId, initialPoints);
        try {
            secondUserCreditService.createPointsRecord(userId, initialPoints, pointsType);
        } catch (Exception e) {
            log.error("新建用户积分记录失败，userId={}, initialPoints={}", userId, initialPoints, e);
        }
    }

    @ApiOperation("用户身份认证成功后增加信用分")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", required = true)})
    @GetMapping("/addIdInfoPoints")
    public R<Boolean> addIdInfoPoints(@RequestParam("userId") Integer userId) throws Exception {
        log.info("SecondUserPointsController.addIdInfoPoints?userId={}", userId);
        return R.data(secondUserCreditService.addIdInfoPoints(userId));
    }

    @ApiOperation("信用分是否达标")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", required = true)})
    @GetMapping("/isFree")
    public R<JSONObject> isFree(@RequestParam("userId") Integer userId) throws Exception {
        log.info("SecondUserPointsController.isFree?userId={}", userId);
        return R.data(secondUserCreditService.isFree(userId));
    }
}