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.FollowRequestDTO;
import cn.meowrain.aioj.backend.blogservice.service.FollowService;
import com.baomidou.mybatisplus.core.metadata.IPage;
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/follow")
@Tag(name = "用户关注管理", description = "关注、取消关注、查询关注列表等接口")
public class FollowController {

    private final FollowService followService;

    @PostMapping("/follow")
    @Operation(summary = "关注用户", description = "关注指定用户")
    public Result<Void> followUser(
            @Parameter(description = "关注信息", required = true)
            @Valid @RequestBody FollowRequestDTO request) {
        followService.followUser(request);
        return Results.success();
    }

    @DeleteMapping("/unfollow/{followingId}")
    @Operation(summary = "取消关注用户", description = "取消关注指定用户")
    public Result<Void> unfollowUser(
            @Parameter(description = "被关注者ID", required = true)
            @PathVariable("followingId") Long followingId) {
        followService.unfollowUser(followingId);
        return Results.success();
    }

    @GetMapping("/check/{followingId}")
    @Operation(summary = "查询关注状态", description = "查询当前用户是否关注了指定用户")
    public Result<Boolean> isFollowing(
            @Parameter(description = "被关注者ID", required = true)
            @PathVariable("followingId") Long followingId) {
        Long currentUserId = cn.meowrain.aioj.backend.framework.core.utils.ContextHolderUtils.getCurrentUserId();
        Boolean following = followService.isFollowing(currentUserId, followingId);
        return Results.success(following);
    }

    @GetMapping("/following/list")
    @Operation(summary = "查询关注列表", description = "分页查询当前用户关注的人")
    public Result<IPage<Long>> getFollowingList(
            @Parameter(description = "用户ID", required = true)
            @RequestParam("userId") Long userId,
            @Parameter(description = "页码", required = true)
            @RequestParam("current") Integer current,
            @Parameter(description = "每页数量", required = true)
            @RequestParam("size") Integer size) {
        IPage<Long> page = followService.getFollowingList(userId, current, size);
        return Results.success(page);
    }

    @GetMapping("/followers/list")
    @Operation(summary = "查询粉丝列表", description = "分页查询当前用户的粉丝")
    public Result<IPage<Long>> getFollowerList(
            @Parameter(description = "用户ID", required = true)
            @RequestParam("userId") Long userId,
            @Parameter(description = "页码", required = true)
            @RequestParam("current") Integer current,
            @Parameter(description = "每页数量", required = true)
            @RequestParam("size") Integer size) {
        IPage<Long> page = followService.getFollowerList(userId, current, size);
        return Results.success(page);
    }

    @GetMapping("/following/count")
    @Operation(summary = "查询关注数", description = "获取用户的关注数量")
    public Result<Long> getFollowingCount(
            @Parameter(description = "用户ID", required = true)
            @RequestParam("userId") Long userId) {
        Long count = followService.getFollowingCount(userId);
        return Results.success(count);
    }

    @GetMapping("/followers/count")
    @Operation(summary = "查询粉丝数", description = "获取用户的粉丝数量")
    public Result<Long> getFollowerCount(
            @Parameter(description = "用户ID", required = true)
            @RequestParam("userId") Long userId) {
        Long count = followService.getFollowerCount(userId);
        return Results.success(count);
    }
}
