package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.reward.RewardCreateRequest;
import com.emotion.dto.request.reward.RewardPageRequest;
import com.emotion.dto.request.reward.RewardUpdateRequest;
import com.emotion.dto.response.reward.RewardResponse;
import com.emotion.service.RewardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 奖励控制器
 *
 * @author huazhongmin
 * @date 2025-09-08
 */
@RestController
@RequestMapping("/reward")
public class RewardController {

    @Autowired
    private RewardService rewardService;

    /**
     * 分页查询奖励
     */
    @GetMapping(value = "/page")
    public Result<PageResult<RewardResponse>> getPage(@Validated RewardPageRequest request) {
        PageResult<RewardResponse> pageResult = rewardService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取奖励
     */
    @GetMapping(value = "/detail")
    public Result<RewardResponse> getById(@RequestParam String id) {
        RewardResponse response = rewardService.getRewardResponseById(id);
        if (response == null) {
            return Result.notFound("奖励不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建奖励
     */
    @PostMapping(value = "/create")
    public Result<RewardResponse> create(@Valid @RequestBody RewardCreateRequest request) {
        RewardResponse response = rewardService.createRewardWithResponse(request);
        return Result.success(response);
    }

    /**
     * 更新奖励
     */
    @PutMapping(value = "/update")
    public Result<RewardResponse> update(@Valid @RequestBody RewardUpdateRequest request) {
        RewardResponse response = rewardService.updateRewardWithResponse(request);
        if (response == null) {
            return Result.notFound("奖励不存在");
        }
        return Result.success(response);
    }

    /**
     * 删除奖励
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = rewardService.deleteReward(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}