package com.xindazhou.business.controller.app.community.like;

import com.xindazhou.business.controller.app.community.like.vo.LikeReqVO;
import com.xindazhou.business.service.community.like.LikeService;
import com.xindazhou.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 移动端 - 点赞 Controller
 *
 * @author xindazhou
 */
@Tag(name = "移动端 - 点赞管理")
@RestController
@RequestMapping("/app-api/community/like")
@Validated
@Slf4j
public class LikeController {

    @Resource
    private LikeService likeService;

    @PostMapping("/toggle")
    @Operation(summary = "点赞/取消点赞")
    public CommonResult<Boolean> toggleLike(@Valid @RequestBody LikeReqVO reqVO) {
        Boolean isLiked = likeService.toggleLike(reqVO.getTargetType(), reqVO.getTargetId());
        return success(isLiked);
    }

    @GetMapping("/check")
    @Operation(summary = "查询是否已点赞")
    @Parameter(name = "targetType", description = "目标类型：1-帖子，2-评论", required = true, example = "1")
    @Parameter(name = "targetId", description = "目标ID", required = true, example = "1")
    public CommonResult<Boolean> isLiked(@RequestParam("targetType") Integer targetType,
                                           @RequestParam("targetId") Long targetId) {
        Boolean isLiked = likeService.isLiked(targetType, targetId);
        return success(isLiked);
    }

    @GetMapping("/count")
    @Operation(summary = "统计点赞数")
    @Parameter(name = "targetType", description = "目标类型：1-帖子，2-评论", required = true, example = "1")
    @Parameter(name = "targetId", description = "目标ID", required = true, example = "1")
    public CommonResult<Long> getLikeCount(@RequestParam("targetType") Integer targetType,
                                            @RequestParam("targetId") Long targetId) {
        Long count = likeService.getLikeCount(targetType, targetId);
        return success(count);
    }

}

