package shop.alien.store.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.store.UserActionRewardRule;
import shop.alien.entity.store.vo.RewardRuleTreeNodeVo;
import shop.alien.store.service.UserActionRewardRuleService;

import java.util.List;

/**
 * 用户行为奖励规则配置表 前端控制器
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"用户行为奖励规则管理"})
@CrossOrigin
@RestController
@RequestMapping("/userActionRewardRule")
@RequiredArgsConstructor
public class UserActionRewardRuleController {

    private final UserActionRewardRuleService userActionRewardRuleService;

    @ApiOperation("分页查询奖励规则列表")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", paramType = "query", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", dataType = "int", paramType = "query", required = true, defaultValue = "10"),
            @ApiImplicitParam(name = "role", value = "角色", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "action", value = "行为", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "reward", value = "奖励", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "status", value = "状态：0-禁用，1-启用", dataType = "int", paramType = "query", required = false)
    })
    @GetMapping("/getRewardRuleList")
    public R<List<RewardRuleTreeNodeVo>> getRewardRuleList(
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "size", defaultValue = "10") int size,
            @RequestParam(value = "role", required = false) String role,
            @RequestParam(value = "action", required = false) String action,
            @RequestParam(value = "reward", required = false) String reward,
            @RequestParam(value = "status", required = false) Integer status) {
        log.info("UserActionRewardRuleController.getRewardRuleList?page={}, size={}, role={}, action={}, reward={}, status={}", 
                page, size, role, action, reward, status);
        return R.data(userActionRewardRuleService.getRewardRuleList(page, size, role, action, reward, status));
    }

    @ApiOperation("根据ID获取奖励规则详情")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/getRewardRuleById")
    public R<UserActionRewardRule> getRewardRuleById(@RequestParam("id") Integer id) {
        log.info("UserActionRewardRuleController.getRewardRuleById?id={}", id);
        UserActionRewardRule rewardRule = userActionRewardRuleService.getRewardRuleById(id);
        if (rewardRule == null) {
            return R.fail("奖励规则不存在或已删除");
        }
        return R.data(rewardRule);
    }

    @ApiOperation("新增或更新奖励规则")
    @ApiOperationSupport(order = 3)
    @PostMapping("/addOrUpdateRewardRule")
    public R<String> addOrUpdateRewardRule(@RequestBody UserActionRewardRule rewardRule) {
        log.info("UserActionRewardRuleController.addOrUpdateRewardRule?rewardRule={}", rewardRule);
        return userActionRewardRuleService.addOrUpdateRewardRule(rewardRule);
    }

    @ApiOperation("删除奖励规则")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/deleteRewardRuleById")
    public R<String> deleteRewardRuleById(@RequestParam("id") Integer id) {
        log.info("UserActionRewardRuleController.deleteRewardRuleById?id={}", id);
        return userActionRewardRuleService.deleteRewardRuleById(id);
    }

    @ApiOperation("修改奖励规则状态")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "status", value = "状态：0-禁用，1-启用", dataType = "int", paramType = "query", required = true)
    })
    @GetMapping("/updateRewardRuleStatus")
    public R<String> updateRewardRuleStatus(
            @RequestParam("id") Integer id,
            @RequestParam("status") Integer status) {
        log.info("UserActionRewardRuleController.updateRewardRuleStatus?id={}, status={}", id, status);
        return userActionRewardRuleService.updateRewardRuleStatus(id, status);
    }
}

