package cn.qinys.platform.mobile.controller;

import cn.qinys.platform.base.response.Result;
import cn.qinys.platform.mobile.req.WishCreateReq;
import cn.qinys.platform.mobile.resp.WishDetailResp;
import cn.qinys.platform.mobile.resp.WishListResp;
import cn.qinys.platform.mobile.service.WishService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dgapi/mobile/wish")
public class WishController {

    @Resource
    private WishService wishService;

    /** 创建愿望 */
    @PostMapping("/create")
    public Result<WishDetailResp> create(@RequestBody @Valid WishCreateReq req,
                                         @RequestHeader(value = "X-User-Id", defaultValue = "anonymous") String userId) {
        WishDetailResp resp = wishService.create(req, userId);
        return new Result<>(resp);
    }

    /** 许愿树下的公开愿望列表 */
    @GetMapping("/tree/{treeId}")
    public Result<WishListResp> listByTree(@PathVariable Long treeId,
                                           @RequestParam(defaultValue = "1") Integer page,
                                           @RequestParam(defaultValue = "20") Integer pageSize) {
        WishListResp resp = wishService.listByTree(treeId, page, pageSize);
        return new Result<>(resp);
    }

    /** 我的愿望列表 */
    @GetMapping("/my")
    public Result<WishListResp> myList(@RequestHeader(value = "X-User-Id", defaultValue = "anonymous") String userId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "20") Integer pageSize) {
        WishListResp resp = wishService.listMyWishes(userId, page, pageSize);
        return new Result<>(resp);
    }

    /** 愿望详情 */
    @GetMapping("/{id}")
    public Result<WishDetailResp> detail(@PathVariable Long id) {
        WishDetailResp resp = wishService.getDetail(id);
        return resp != null ? new Result<>(resp) : new Result<>(404, "愿望不存在");
    }

    /** 删除愿望 */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id,
                                  @RequestHeader(value = "X-User-Id", defaultValue = "anonymous") String userId) {
        boolean ok = wishService.delete(id, userId);
        return ok ? new Result<>(true) : new Result<>(404, "删除失败，愿望不存在或无权操作");
    }

    /** 点赞愿望 */
    @PostMapping("/{id}/like")
    public Result<Integer> like(@PathVariable Long id) {
        int count = wishService.like(id);
        return new Result<>(count);
    }
}
