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.req.WishPageReq;
import cn.qinys.platform.mobile.resp.WishDetailResp;
import cn.qinys.platform.mobile.resp.WishPageResp;
import cn.qinys.platform.mobile.service.UserWishService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

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

    @Resource
    private UserWishService wishService;

    /**
     * 创建愿望
     */
    @PostMapping("/create")
    public Result<String> create(@RequestBody @Valid WishCreateReq req) {
        wishService.create(req);
        return new Result<>("success");
    }

    /**
     * 许愿树下的公开愿望列表
     */
    @GetMapping("/tree/{treeId}")
    public Result<Page<WishPageResp>> listByTree(@PathVariable("treeId") Integer treeId, WishPageReq req) {
        Page<WishPageResp> resp = wishService.pageByTree(treeId, req);
        return new Result<>(resp);
    }

    /**
     * 我的愿望列表
     */
    @GetMapping("/my")
    public Result<Page<WishPageResp>> myWishPage(WishPageReq req) {
        Page<WishPageResp> resp = wishService.pageByUser(req);
        return new Result<>(resp);
    }

    /**
     * 愿望详情
     */
    @GetMapping("/{id}")
    public Result<WishDetailResp> detail(@PathVariable("id") Integer id) {
        WishDetailResp resp = wishService.getDetail(id);
        return new Result<>(resp);
    }

    /**
     * 删除愿望
     */
    @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);
    }
}
