package com.red.circle.other.adapter.app.room;

import com.red.circle.common.business.core.ControllerRedisKeyConstant;
import com.red.circle.common.business.dto.cmd.app.AppFlowCmd;
import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.app.dto.clientobject.room.RoomBrowseRecordsV2CO;
import com.red.circle.other.app.dto.clientobject.room.RoomCheckStatusCO;
import com.red.circle.other.app.dto.cmd.room.RoomSubscriptionCmd;
import com.red.circle.other.app.service.room.RoomRelationService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
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;

/**
 * <p>
 * 房间关系 前端控制器.
 * </p>
 *
 * @author houyong on 2020/12/01
 * @eo.api-type http
 * @eo.groupName 直播服务.房间关系
 * @eo.path /room/relation
 * @since pengliang on 2023/9/21
 */
@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping(value = "/room/relation", produces = MediaType.APPLICATION_JSON_VALUE)
public class RelationRoomRestController extends BaseController {

  private final RoomRelationService roomRelationService;

  /**
   * 我的足迹.
   *
   * @eo.name 我的足迹.
   * @eo.url /trace
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/trace")
  public List<RoomBrowseRecordsV2CO> trace(AppFlowCmd cmd) {
    return roomRelationService.listUserTrace(cmd);
  }

  /**
   * 上传足迹.
   *
   * @eo.name 上传足迹.
   * @eo.url /trace/upload
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/trace/upload")
  public void uploadTrace(AppRoomIdCmd cmd) {
    roomRelationService.uploadTrace(cmd);
  }

  /**
   * 我的加入.
   *
   * @eo.name 我的加入.
   * @eo.url /joined
   * @eo.method get
   * @eo.request-type formdata
   */
  @Cacheable(value = ControllerRedisKeyConstant.ROOM_JOINED_LIST, key = "#cmd.requiredReqUserId()", unless = "#result == null || #result.isEmpty()", condition = "#cmd.checkCache()")
  @GetMapping("/joined")
  public List<RoomBrowseRecordsV2CO> joined(AppFlowCmd cmd) {
    return roomRelationService.listUserJoined(cmd);
  }

  /**
   * 我的关注.
   *
   * @eo.name 我的关注.
   * @eo.url /follow
   * @eo.method get
   * @eo.request-type formdata
   */
  @Cacheable(value = ControllerRedisKeyConstant.ROOM_FOLLOW_LIST, key = "#cmd.requiredReqUserId()", unless = "#result == null || #result.isEmpty()", condition = "#cmd.checkCache()")
  @GetMapping("/follow")
  public List<RoomBrowseRecordsV2CO> follow(AppFlowCmd cmd) {
    return roomRelationService.listUserFollow(cmd);
  }


  /**
   * 推荐房间
   */
  @GetMapping("/recommend")
  public List<RoomBrowseRecordsV2CO> recommend(AppFlowCmd cmd) {
    return roomRelationService.listUserRecommend(cmd);
  }

  /**
   * 关注/取消关注.
   *
   * @eo.name 关注/取消关注.
   * @eo.url /follow/action
   * @eo.method post
   * @eo.request-type json
   */
  @CacheEvict(value = ControllerRedisKeyConstant.ROOM_FOLLOW_LIST, key = "#cmd.requiredReqUserId()")
  @PostMapping("/follow/action")
  public Boolean addOrDelete(@RequestBody @Validated RoomSubscriptionCmd cmd) {
   return roomRelationService.followOrCancelRoom(cmd);
  }

  /**
   * 检测我是否有关注/加入房间.
   *
   * @eo.name 检测我是否有关注/加入房间.
   * @eo.url /status
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/status")
  public RoomCheckStatusCO checkStatus(AppRoomIdCmd cmd) {
    return roomRelationService.checkStatus(cmd);
  }
}
