package shop.alien.second.controller;

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.SecondUserCreditRecord;
import shop.alien.entity.second.vo.SecondUserCreditRecordListVo;
import shop.alien.second.service.SecondUserCreditRecordService;

import java.util.List;

/**
 * 二手平台用户积分记录控制器
 */
@Slf4j
@Api(tags = {"二期-用户积分记录"})
@CrossOrigin
@RestController
@RequestMapping("/userPointsRecord")
@RequiredArgsConstructor
public class SecondUserCreditRecordController {
    
    private final SecondUserCreditRecordService secondUserPointsRecordService;
    
    @ApiOperation("获取用户积分记录列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "path", required = true)
    })
    @GetMapping("/user/{userId}")
    public R<List<SecondUserCreditRecord>> getUserPointsRecords(@PathVariable Integer userId) {
        log.info("获取用户积分记录列表，userId={}", userId);
        try {
            List<SecondUserCreditRecord> records = secondUserPointsRecordService.getRecordsByUserId(userId);
            return R.data(records);
        } catch (Exception e) {
            log.error("获取用户积分记录列表失败，userId={}", userId, e);
            return R.fail("获取用户积分记录列表失败");
        }
    }

    @ApiOperation("根据用户ID查询用户信用分记录")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "timeRange", value = "时间范围：1-近一周，2-近一个月，3-近一年", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pointsType", value = "积分类型：0-全部，1-正分，2-负分", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getByUserId")
    public R<SecondUserCreditRecordListVo> getByUserId(
            @RequestParam Integer userId,
            @RequestParam(required = false) Integer timeRange,
            @RequestParam(required = false) Integer pointsType) {
        return secondUserPointsRecordService.getByUserId(userId, timeRange, pointsType);
    }
    
    @ApiOperation("创建积分记录")
    @ApiOperationSupport(order = 2)
    @PostMapping("/create")
    public R<Boolean> createPointsRecord(@RequestBody SecondUserCreditRecord record) {
        log.info("创建积分记录，userId={}, points={}", record.getUserId(), record.getPoints());
        try {
            boolean result = secondUserPointsRecordService.createRecord(record);
            if (result) {
                return R.success("积分记录创建成功");
            } else {
                return R.fail("积分记录创建失败");
            }
        } catch (Exception e) {
            log.error("创建积分记录失败，record={}", record, e);
            return R.fail("积分记录创建失败");
        }
    }

    @ApiOperation("用户观看完学习视频后增加积分")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "learningId", value = "学习视频ID", dataType = "Integer", paramType = "query", required = true)
    })
    @PostMapping("/addPointsAfterWatchingVideo")
    public R<String> addPointsAfterWatchingVideo(
            @RequestParam Integer userId,
            @RequestParam Integer learningId) {
        return secondUserPointsRecordService.addPointsAfterWatchingVideo(userId, learningId);
    }
}