package cn.reghao.tnb.user.app.controller;

import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.auth.api.iface.AccountQuery;
import cn.reghao.tnb.common.util.ConstantId;
import cn.reghao.tnb.user.api.dto.UserCard;
import cn.reghao.tnb.common.auth.AuthUser;
import cn.reghao.tnb.common.auth.UserContext;
import cn.reghao.tnb.user.app.service.UserRelationService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2022-01-14 18:50:16
 */
@Tag(name = "用户关注接口")
@RestController
@RequestMapping("/api/user/relation")
public class UserRelationController {
    @DubboReference(check = false, retries = 0, timeout = 60_000)
    private AccountQuery accountQuery;

    private final UserRelationService userRelationService;

    public UserRelationController(UserRelationService userRelationService) {
        this.userRelationService = userRelationService;
    }

    @Operation(summary = "关注用户", description = "N")
    @PostMapping("/follow/{followingId}")
    public String followUser(@PathVariable("followingId") String followingId) {
        long followingId1 = accountQuery.getUserIdLong(followingId);
        long loginUser = UserContext.getUserId();
        userRelationService.followUser(loginUser, followingId1);
        return WebResult.success();
    }

    @AuthUser
    @Operation(summary = "取消关注用户", description = "N")
    @PostMapping("/unfollow/{followingId}")
    public String unfollowUser(@PathVariable("followingId") String followingId) {
        long followingId1 = accountQuery.getUserIdLong(followingId);
        long loginUser = UserContext.getUserId();
        userRelationService.unfollowUser(loginUser, followingId1);
        return WebResult.success();
    }

    @Operation(summary = "检查是否关注了用户", description = "N")
    @GetMapping("/check/{userId}")
    public String check(@PathVariable("userId") String userId) {
        long userId1 = accountQuery.getUserIdLong(userId);
        long loginUser = UserContext.getUserId();
        boolean followed;
        if (loginUser == ConstantId.ANONYMOUS_USER_ID) {
            followed = false;
        } else {
            followed = userRelationService.isFollowing(loginUser, userId1);
        }
        return WebResult.success(followed);
    }

    @Operation(summary = "获取用户的关注列表", description = "N")
    @GetMapping("/following/{userId}")
    public String getFollowing(@PathVariable("userId") String userId) {
        long userId1 = accountQuery.getUserIdLong(userId);
        List<UserCard> list = userRelationService.getFollowingCards(userId1);
        return WebResult.success(list);
    }

    @Operation(summary = "获取用户的粉丝列表", description = "N")
    @GetMapping("/follower/{userId}")
    public String getFollower(@PathVariable("userId") String userId) {
        long userId1 = accountQuery.getUserIdLong(userId);
        List<UserCard> list = userRelationService.getFollowerCards(userId1);
        return WebResult.success(list);
    }
}
