package org.lym.pom.controller;

import jakarta.validation.Valid;
import org.lym.pom.dto.posts.FollowDTO;
import org.lym.pom.repository.mongo.Follow;
import org.lym.pom.service.FollowService;
import org.shoulder.core.dto.request.BasePageQuery;
import org.shoulder.core.dto.response.BaseResult;
import org.shoulder.core.dto.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/follow")
public class FollowController {

    @Autowired
    private FollowService followService;

    /**
     * 检查用户A是否关注了用户B
     *
     * @param request 包含关注者与被关注者ID的请求体
     * @return 是否关注的结果
     */
    @PostMapping("check")
    public BaseResult<Boolean> checkFollow(@RequestBody @Valid FollowDTO request) {
        String userId = request.getUserId();
        String followUserId = request.getFollowUserId();
        boolean isFollowed = followService.isFollow(userId, followUserId);
        return BaseResult.success(isFollowed);
    }

    /**
     * 关注某个用户
     *
     * @param request 包含关注者与被关注者ID的请求体
     */
    @PostMapping("follow")
    public BaseResult<Void> follow(@RequestBody @Valid FollowDTO request) {
        String userId = request.getUserId();
        String followUserId = request.getFollowUserId();
        followService.follow(userId, followUserId);
        return BaseResult.success();
    }

    /**
     * 取消关注某个用户
     *
     * @param request 包含关注者与被关注者ID的请求体
     */
    @PostMapping("unfollow")
    public BaseResult<Void> unfollow(@RequestBody @Valid FollowDTO request) {
        String userId = request.getUserId();
        String followUserId = request.getFollowUserId();
        followService.unfollow(userId, followUserId);
        return BaseResult.success();
    }

    /**
     * 查询某个用户的关注列表
     *
     * @param pageQuery 分页查询参数
     * @return 分页结果
     */
    @GetMapping("page/follows")
    public BaseResult<PageResult<Follow>> pageQueryFollows(@RequestBody @Valid BasePageQuery<Follow> pageQuery) {
        PageResult<Follow> result = followService.pageQueryFollows(pageQuery);
        return BaseResult.success(result);
    }

    /**
     * 查询某个用户的被关注列表
     */
    @PostMapping("page/followed")
    public BaseResult<PageResult<Follow>> pageQueryFollowed(@RequestBody @Valid BasePageQuery<Follow> pageQuery) {
        PageResult<Follow> result = followService.pageQueryFollows(pageQuery);
        return BaseResult.success(result);
    }
}