package cn.meowrain.aioj.backend.blogservice.controller;

import cn.meowrain.aioj.backend.framework.core.web.Result;
import cn.meowrain.aioj.backend.framework.core.web.Results;
import cn.meowrain.aioj.backend.blogservice.dto.req.LikeRequestDTO;
import cn.meowrain.aioj.backend.blogservice.service.LikeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 点赞管理控制器
 *
 * @author AIOJ
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/v1/like")
@Tag(name = "点赞管理", description = "文章和评论的点赞、取消点赞等接口")
public class LikeController {

    private final LikeService likeService;

    @PostMapping("/like")
    @Operation(summary = "点赞", description = "对文章或评论进行点赞")
    public Result<Void> like(
            @Parameter(description = "点赞信息", required = true)
            @Valid @RequestBody LikeRequestDTO request) {
        likeService.like(request);
        return Results.success();
    }

    @PostMapping("/unlike")
    @Operation(summary = "取消点赞", description = "取消对文章或评论的点赞")
    public Result<Void> unlike(
            @Parameter(description = "点赞信息", required = true)
            @Valid @RequestBody LikeRequestDTO request) {
        likeService.unlike(request);
        return Results.success();
    }

    @GetMapping("/check")
    @Operation(summary = "查询点赞状态", description = "查询当前用户对指定目标的点赞状态")
    public Result<Boolean> isLiked(
            @Parameter(description = "目标ID", required = true)
            @RequestParam("targetId") Long targetId,
            @Parameter(description = "目标类型：1=文章，2=评论", required = true)
            @RequestParam("targetType") Integer targetType) {
        Long currentUserId = cn.meowrain.aioj.backend.framework.core.utils.ContextHolderUtils.getCurrentUserId();
        Boolean liked = likeService.isLiked(currentUserId, targetId, targetType);
        return Results.success(liked);
    }

    @PostMapping("/toggle")
    @Operation(summary = "切换点赞状态", description = "点赞或取消点赞，根据当前状态自动切换")
    public Result<Void> toggleLike(
            @Parameter(description = "点赞信息", required = true)
            @Valid @RequestBody LikeRequestDTO request) {
        likeService.toggleLike(request);
        return Results.success();
    }
}
