package com.red.circle.game.fruit.application.command.query;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.game.fruit.application.dto.enums.MessageTypeTopic;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitUserTaskReceiveService;
import com.red.circle.game.fruit.infrastructure.websocket.message.MessageSender;
import com.red.circle.game.fruit.infrastructure.websocket.message.TopicMessage;
import com.red.circle.other.inner.asserts.user.UserErrorCode;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.other.inner.endpoint.user.user.UserProfileClient;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 领取了奖励的人-limit20
 *
 * @author zongpubin on 2023/12/18
 */
@Component
@RequiredArgsConstructor
public class GameFruitReceiveUserQryExe {

  private final MessageSender messageSender;
  private final UserProfileClient userProfileClient;
  private final GameFruitUserTaskReceiveService gameFruitUserTaskReceiveService;

  public void execute(Long userId) {

    // 用户数据
    UserProfileDTO userProfileDTO = ResponseAssert.requiredSuccess(
        userProfileClient.getByUserId(userId));

    if (Objects.isNull(userProfileDTO)) {
      messageSender.sendTopic(
          TopicMessage.of(
              MessageTypeTopic.GAME_FRUIT_RECEIVE_USER_INFO_MSG.getTopicId(userId),
              ResultResponse.failure(
                  UserErrorCode.USER_INFO_NOT_FOUND)));
      return;
    }

    Set<Long> userIds = gameFruitUserTaskReceiveService.getReceiveUsers(
        userProfileDTO.getOriginSys());

    List<UserProfileDTO> userProfiles = ResponseAssert.requiredSuccess(
        userProfileClient.listByUserId(userIds));

    messageSender.sendTopic(
        TopicMessage.of(
            MessageTypeTopic.GAME_FRUIT_RECEIVE_USER_INFO_MSG.getTopicId(userId),
            ResultResponse.success(userProfiles)
        ));


  }
}
