package com.red.circle.other.app.command.team.query;

import com.red.circle.common.business.dto.cmd.app.AppAccountCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.other.app.convertor.team.TeamAppConvertor;
import com.red.circle.other.app.convertor.user.UserProfileAppConvertor;
import com.red.circle.other.app.dto.clientobject.team.TeamProfileSearchResCO;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.database.mongo.entity.team.team.TeamProfile;
import com.red.circle.other.infra.database.mongo.service.team.team.TeamProfileService;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import com.red.circle.other.inner.asserts.team.TeamErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 根据账号获取团队资料信息.
 *
 * @author pengliang on 2022/12/29
 */
@Component
@RequiredArgsConstructor
public class TeamProfileByAccountQryExe {

  private final TeamAppConvertor teamAppConvertor;
  private final UserProfileGateway userProfileGateway;
  private final TeamProfileService teamProfileService;
  private final UserProfileAppConvertor userProfileAppConvertor;

  public TeamProfileSearchResCO execute(AppAccountCmd cmd) {
    UserProfileDTO userProfile = userProfileAppConvertor.toUserProfileDTO(
        userProfileGateway.getByAccount(cmd.requireReqSysOrigin(),
            cmd.getAccount()));
    ResponseAssert.notNull(TeamErrorCode.TEAM_NOT_FOUND, userProfile);

    TeamProfile profile = teamProfileService.getByOwnUserId(userProfile.getId());
    ResponseAssert.notNull(TeamErrorCode.TEAM_NOT_FOUND, profile);

    return new TeamProfileSearchResCO()
        .setTeamProfile(teamAppConvertor.toTeamProfile(profile))
        .setOwnUserProfile(userProfile);
  }

}
