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

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.app.AppAccountCmd;
import com.red.circle.common.business.dto.cmd.app.AppIdLongCmd;
import com.red.circle.common.business.dto.cmd.app.AppIdStringCmd;
import com.red.circle.common.business.dto.cmd.app.AppRoomIdCmd;
import com.red.circle.common.business.dto.cmd.app.AppUserIdCmd;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.app.dto.clientobject.team.*;
import com.red.circle.other.app.dto.cmd.team.*;
import com.red.circle.other.app.service.team.TeamService;
import com.red.circle.other.infra.database.mongo.entity.team.bd.MessageInviteGroup;
import com.red.circle.other.infra.database.rds.service.sys.AdministratorAuthService;
import com.red.circle.other.inner.asserts.team.TeamErrorCode;
import com.red.circle.other.inner.endpoint.team.target.TeamProfileClient;
import com.red.circle.other.inner.enums.team.TeamUserIdentity;
import com.red.circle.other.inner.model.cmd.team.TeamCreateCmd;
import com.red.circle.other.inner.model.dto.user.UserProfileDTO;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 团队服务(主播代理).
 *
 * @author pengliang on 2022/12/6
 * @eo.api-type http
 * @eo.groupName 团队服务.主播代理
 * @eo.path /team
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/team",produces = MediaType.APPLICATION_JSON_VALUE)
public class TeamRestController extends BaseController {

  private final TeamService teamService;
  private final TeamProfileClient teamProfileClient;
  private final AdministratorAuthService administratorAuthService;

  /**
   * 成员进入团队.
   *
   * @eo.name 成员进入团队.
   * @eo.url /entry
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/entry")
  public MemberEntryTeamCO entryTeam(AppExtCommand cmd) {
    return teamService.entryTeam(cmd);
  }

  /**
   * 获取成员角色.
   *
   * @eo.name 获取成员角色.
   * @eo.url /member/role
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/member/role")
  public String getMemberRole(AppIdLongCmd cmd) {
    return teamService.getMemberRole(cmd);
  }

    /**
     * 获取用户信息
     */
  @GetMapping("/member/profile")
  public UserProfileDTO getMemberProfile(AppIdStringCmd cmd) {
      return teamService.getMemberProfile(cmd);
  }

  /**
   * 获取团队成员列表.
   *
   * @eo.name 获取团队成员列表.
   * @eo.url /members
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/members")
  public List<TeamMemberCO> listTeamMember(@Validated AppIdLongCmd cmd) {
    return teamService.listTeamMember(cmd);
  }

    /**
     * 搜索团队成员列表.
     */
    @GetMapping("/search-members")
    public List<TeamMemberCO> searchTeamMember(AppExtCommand cmd, @RequestParam String account) {
        return teamService.searchTeamMember(cmd.getReqUserId(), account);
    }


    /**
   * 成员数量
   */
  @GetMapping("/members/count")
  public TeamMemberCountResultCO countTeamMember(@Validated AppIdLongCmd cmd) {
    return teamService.countTeamMember(cmd);
  }

  /**
   * 获取我的团队成员列表.
   *
   * @eo.name 获取团队成员列表.
   * @eo.url /own/members
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/own/members")
  public List<TeamOwnMemberCO> listTeamMember(AppExtCommand cmd) {
    return teamService.listTeamMember(cmd);
  }

  /**
   * 成员工资情况.
   *
   * @eo.name 成员工资情况.
   * @eo.url /member-work
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/member-work")
  public TeamMemberWorkCO memberWork(@Validated AppUserIdCmd cmd) {
    return teamService.getMemberWork(cmd);
  }

  /**
   * 结算结果.
   *
   * @eo.name 结算结果.
   * @eo.url /settlement/result
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/settlement/result")
  public TeamMemberTargetSettlementResultCO getSettlementWages(AppIdStringCmd cmd) {
    return teamService.getSettlementResult(cmd);
  }

  /**
   * 获取最新进行中目标.
   *
   * @eo.name 获取最新进行中目标.
   * @eo.url /latest/in-progress/target
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/latest/in-progress/target")
  public String getLatestInProgressTarget(AppExtCommand cmd) {
    return teamService.getLatestInProgressTarget(cmd);
  }

  /**
   * 团队账单.
   *
   * @eo.name 团队账单.
   * @eo.url /bill
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/bill")
  public List<TeamBillCO> listTeamBill(@Validated AppIdLongCmd cmd) {
    return teamService.listTeamBill(cmd);
  }

  /**
   * 团队账单.
   *
   * @eo.name 团队账单.
   * @eo.url /bill-id
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/bill-id")
  public TeamBillCO getTeamBillByBillId(@Validated AppIdLongCmd cmd) {
    return teamService.getTeamBillByBillId(cmd);
  }

  /**
   * 团队账单-工资成员.
   *
   * @eo.name 团队账单-工资成员.
   * @eo.url /bill/work-members
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/bill/work-members")
  public List<TeamBillMemberWorkCO> listTeamBillWorkMembers(@Validated AppIdLongCmd cmd) {
    return teamService.listTeamBillWorkMembers(cmd);
  }

  /**
   * 用户申请加入或退出团队.
   *
   * @eo.name 用户申请加入或退出团队.
   * @eo.url /user/apply
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/user/apply")
  public String userApplyTeam(@RequestBody @Validated TeamUserApplyCmd cmd) {
    return teamService.userApplyTeam(cmd);
  }

  /**
   * 用户撤销退出申请消息.
   *
   * @eo.name 用户撤销退出申请消息.
   * @eo.url /user/apply-cancel
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/user/apply-cancel")
  public void cancelUserApply(@RequestBody @Validated AppIdLongCmd cmd) {
    teamService.cancelUserApplyById(cmd);
  }

  /**
   * 用户撤销加入申请消息.
   *
   * @eo.name 用户撤销加入申请消息.
   * @eo.url /user/join-apply-cancel
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/user/join-apply-cancel")
  public void cancelUserJoinApply(@RequestBody @Validated AppIdLongCmd cmd) {
    teamService.cancelUserJoinApplyById(cmd);
  }

  /**
   * 用户申请记录查询.
   *
   * @eo.name 用户申请记录查询.
   * @eo.url /user/apply/record
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/user/apply/record")
  public List<TeamUserApplyRecordCO> listUserApplyRecord(
      @Validated TeamUserApplyRecordQryCmd cmd) {
    return teamService.listUserApplyRecord(cmd);
  }

  /**
   * 获取团队等待审核记录数.
   *
   * @eo.name 获取团队等待审核记录数.
   * @eo.url /count/wait-apply
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/count/wait-apply")
  public String countWaitApply(@Validated AppIdLongCmd cmd) {
    return teamService.countTeamWaitApply(cmd);
  }

  /**
   * 获取用户退出申请信息.
   *
   * @eo.name 获取用户退出申请信息.
   * @eo.url /get/user-quit/wait-apply-info
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/get/user-quit/wait-apply-info")
  public TeamQuitApplyInfoCO getUserQuitWaitApplyInfo(@Validated AppExtCommand cmd) {
    return teamService.getUserQuitWaitApplyInfo(cmd);
  }

  /**
   * 获取用户等待申请团队信息.
   *
   * @eo.name 获取用户等待申请团队信息.
   * @eo.url /wait-apply/profile
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/wait-apply/profile")
  public TeamWaitApplyMessageCO getWaitApplyTeamProfile(AppExtCommand cmd) {
    return teamService.getWaitApplyTeamProfile(cmd);
  }

  /**
   * 处理用户申请记录.
   *
   * @eo.name 处理用户申请记录.
   * @eo.url /process/user/apply
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/process/user/apply")
  public String processUserApply(@RequestBody @Validated TeamHandleUserApplyCmd cmd) {
    return teamService.processUserApply(cmd);
  }

  /**
   * 系统最新队外发布政策.
   *
   * @eo.name 系统最新队外发布政策.
   * @eo.url /release/policy
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/release/policy")
  public TeamPolicyManagerCO releasePolicy(@Validated AppIdLongCmd cmd) {
    return teamService.getReleasePolicy(cmd);
  }

  /**
   * 搜索团队信息.
   *
   * @eo.name 搜索团队信息.
   * @eo.url /search-account
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/search-account")
  public TeamProfileSearchResCO searchTeamByAccount(@Validated AppAccountCmd cmd) {
    return teamService.searchTeamByAccount(cmd);
  }

  /**
   * 修改成员角色.
   *
   * @eo.name 修改成员角色.
   * @eo.url /member/change-role
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/member/change-role")
  public void changeMemberRole(@RequestBody @Validated TeamMemberRoleChangeCmd cmd) {
    teamService.changeMemberRole(cmd);
  }

  /**
   * 团队成员钱包提现设置.
   *
   * @eo.name 团队成员钱包提现设置.
   * @eo.url /member/wallet-withdrawal
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/member/wallet-withdrawal")
  public void changeWalletWithdrawal(@RequestBody @Validated TeamMemberWalletWithdrawalCmd cmd) {
    teamService.changeWalletWithdrawal(cmd);
  }

  /**
   * 领取政策道具奖励.
   *
   * @eo.name 领取政策道具奖励.
   * @eo.url /policy-props-reward
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/policy-props-reward")
  public void policyPropsReward(@RequestBody @Validated AppIdLongCmd cmd) {
    teamService.policyPropsReward(cmd);
  }

  /**
   * 获取我的团长.
   *
   * @eo.name 获取我的团长.
   * @eo.url /team-own/profile
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/team-own/profile")
  public UserProfileDTO getTeamOwnUserProfile(AppExtCommand cmd) {
    return teamService.getTeamOwnUserProfile(cmd);
  }

  /**
   * 获取我的团队关联BD.
   *
   * @eo.name 获取我的团队关联BD.
   * @eo.url /bd-profile
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/bd-profile")
  public UserProfileDTO getTeamBdProfile(AppExtCommand cmd) {
    return teamService.getTeamBdProfile(cmd);
  }

  /**
   * 获取指定用户团队身份.
   *
   * @eo.name 获取指定用户团队身份.
   * @eo.url /user-identity
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/user-identity")
  public TeamUserIdentity getTeamUserIdentity(AppUserIdCmd cmd) {
    return teamService.getTeamUserIdentity(cmd);
  }

  /**
   * 成员情况统计(App).
   *
   * @eo.name 成员情况统计(App).
   * @eo.url /member/work/statistics
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/member/work/statistics")
  public List<AnchorWorkStatisticsCO> listMemberWorkStatistics(
      @Validated DateTypeQryCmd cmd) {
    return teamService.listMemberWorkStatistics(cmd);
  }

    /**
     * 成员当天目标
     */
    @GetMapping("/member/work/statistics-now")
    public AnchorWorkStatisticsCO nowMemberWorkStatistics(
            @Validated DateTypeQryCmd cmd) {
        return teamService.nowMemberWorkStatistics(cmd);
    }

  /**
   * 检查主播身份.
   *
   * @eo.name 检查主播身份.
   * @eo.url /check/anchor
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/check/anchor")
  public Boolean checkAnchor(AppExtCommand cmd) {
    return teamService.checkAnchor(cmd);
  }

  /**
   * 主播麦位心跳.
   *
   * @eo.name 主播麦位心跳.
   * @eo.url /heartbeat
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/heartbeat")
  public void heartbeat(@Validated AppRoomIdCmd cmd) {
    teamService.heartbeat(cmd);
  }

  /**
   * 获得主播退出团队要求.
   *
   * @eo.name 获得主播退出团队要求.
   * @eo.url /member/quit/ask
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/member/quit/ask")
  public TeamMemberQuitAskCO getMemberQuitAsk(AppExtCommand cmd) {
    return teamService.getMemberQuitAsk(cmd);
  }

  /**
   * 主播中心是否存在消息.
   *
   * @eo.name 主播中心是否存在消息.
   * @eo.url /exists/message/tips
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/exists/message/tips")
  public Boolean existsMessageTips(AppExtCommand cmd) {
    return teamService.existsMessageTips(cmd);
  }

  /**
   * 创建代理
   */
  @PostMapping("/create")
  public void createTeam(@Validated TeamCreateCmd cmd) {
    // APP管理员权限校验
    List<String> authResources = administratorAuthService.listAuthResources(cmd.getReqUserId());
    ResponseAssert.isTrue(TeamErrorCode.ADMIN_PERMISSION_INSUFFICIENT, authResources.contains("TEAM_CREATE"));

    teamProfileClient.createTeam(cmd);
  }

  /**
   * 代理邀请成为主播
   */
  @PostMapping("/invite-host")
  public void inviteAgent(@RequestBody @Validated AgentInviteHostCmd cmd) {
    teamService.inviteHost(cmd);
  }

  /**
   * 代理邀请成为主播-撤销
   */
  @PostMapping("/invite-host-cancel/{id}")
  public void inviteAgentCancel(@PathVariable("id") String id) {
    teamService.inviteHostCancel(id);
  }

  /**
   * 代理邀请成为主播-记录
   */
  @GetMapping("/invite-message-own")
  public List<BdInviteMessageCO> listBdInviteMessageOwn(BdInviteQryCmd cmd) {
    cmd.setGroup(MessageInviteGroup.AGENT_INVITE_HOST.name());
    return teamService.listAgentInviteMessageOwn(cmd);
  }

  /**
   * 代理邀请成为主播-处理
   */
  @PostMapping("/invite-message-process")
  public void processAgentInviteMessage(@RequestBody @Validated AgentInviteMessageProcessCmd cmd) {
    teamService.processBdInviteMessage(cmd);
  }


}
