package com.red.circle.other.app.inner.endpoint.user.account;

import com.red.circle.common.business.enums.AccountStatusEnum;
import com.red.circle.framework.core.dto.CommonCommand;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.other.infra.annotation.CreateAccount;
import com.red.circle.other.inner.endpoint.user.user.api.AppUserAccountClientApi;
import com.red.circle.other.inner.model.cmd.user.ApprovalUserAccountStatusLogCmd;
import com.red.circle.other.inner.model.cmd.user.UserChannelCredentialCmd;
import com.red.circle.other.inner.model.cmd.user.account.*;
import com.red.circle.other.inner.model.dto.user.UserAuthTypeDTO;
import com.red.circle.other.inner.model.dto.user.account.MobileAuthDTO;
import com.red.circle.other.inner.model.dto.user.account.UserAccountDTO;
import com.red.circle.other.inner.model.dto.user.account.UserMobileUpdateCmd;
import com.red.circle.other.app.inner.service.user.user.ApprovalUserAccountClientService;
import com.red.circle.other.app.inner.service.user.user.UserAccountClientService;
import com.red.circle.other.inner.model.cmd.user.UserAccountStatusLogQryCmd;
import com.red.circle.other.inner.model.dto.user.ApprovalUserAccountStatusLogDTO;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 账号管理 服务.
 *
 * @author pengliang on 2023/5/22
 */
@Validated
@RestController
@RequestMapping(value = AppUserAccountClientApi.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class AppUserAccountClientEndpoint implements AppUserAccountClientApi {

  private final UserAccountClientService userAccountClientService;
  private final ApprovalUserAccountClientService approvalUserAccountClientService;

  /**
   * 创建账号.
   */
  @CreateAccount
  @Override
  public ResultResponse<UserAccountDTO> create(@RequestBody @Validated CreateAccountCmd cmd) {
    return ResultResponse.success(userAccountClientService.create(cmd));
  }

  /**
   * 注销账号.
   */
  @Override
  public ResultResponse<Void> logout(CommonCommand cmd) {
    userAccountClientService.logout(cmd);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<Void> signOut(CommonCommand cmd) {
    userAccountClientService.signOut(cmd);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<UserAccountDTO> mobileCredential(MobileCredentialCmd cmd) {
    return ResultResponse.success(userAccountClientService.mobileCredential(cmd));
  }

  @Override
  public ResultResponse<UserAccountDTO> channelCredential(UserChannelCredentialCmd cmd) {
    return ResultResponse.success(userAccountClientService.channelCredential(cmd));
  }

  @Override
  public ResultResponse<Void> unblock(UnblockAccountCmd cmd) {
    userAccountClientService.unblock(cmd);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<Void> punishment(PunishmentAccountCmd cmd) {
    userAccountClientService.punishment(cmd);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<UserAccountDTO> accountLogin(AccountLoginCmd cmd) {
    return ResultResponse.success(userAccountClientService.accountLogin(cmd));
  }

  @Override
  public ResultResponse<UserAuthTypeDTO> getByUserIdOne(Long userId) {
    return ResultResponse.success(userAccountClientService.getByUserIdOne(userId));
  }

  @Override
  public ResultResponse<MobileAuthDTO> getMobileAuthByUserId(Long userId) {
    return ResultResponse.success(userAccountClientService.getMobileAuthByUserId(userId));
  }

  /**
   * 获得用户认证信息列表
   */
  @Override
  public ResultResponse<List<UserAuthTypeDTO>> listByUserId(Long userId) {
    return ResultResponse.success(userAccountClientService.listByUserId(userId));
  }

  @Override
  public ResultResponse<Void> editUserMobileAuth(UserMobileUpdateCmd param) {
    userAccountClientService.editUserMobileAuth(param);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<Void> deleteUserMobileAuth(Long userId) {
    userAccountClientService.deleteUserMobileAuth(userId);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<Void> deleteAccountAuthCacheByUserId(Long userId) {
    userAccountClientService.deleteAccountAuthByUserId(userId);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<Void> deleteUserAccountLogin(Long userId) {
    userAccountClientService.deleteUserAccountLogin(userId);
    return ResultResponse.success();
  }


  @Override
  public ResultResponse<Void> saveApprovalLog(ApprovalUserAccountStatusLogCmd cmd) {
    approvalUserAccountClientService.saveLog(cmd);
    return ResultResponse.success();
  }


  @Override
  public ResultResponse<Boolean> updateAccountFreeze(Long userId, Integer days) {
    return ResultResponse.success(userAccountClientService.updateAccountFreeze(userId, days));
  }


  @Override
  public ResultResponse<Boolean> updateAccountStatus(Long userId, AccountStatusEnum accountStatus) {
    return ResultResponse.success(
        userAccountClientService.updateAccountStatus(userId, accountStatus));
  }

  @Override
  public ResultResponse<Boolean> updateAccountStatusDays(Long userId, AccountStatusEnum accountStatus, Integer days) {
    return ResultResponse.success(
        userAccountClientService.updateAccountStatusDays(userId, accountStatus, days));
  }

  @Override
  public ResultResponse<PageResult<ApprovalUserAccountStatusLogDTO>> pgeApprovalUserAccountStatusLog(
      UserAccountStatusLogQryCmd cmd) {
    return ResultResponse.success(userAccountClientService.pgeApprovalUserAccountStatusLog(cmd));
  }

  @Override
  public ResultResponse<List<ApprovalUserAccountStatusLogDTO>> lastApprovalUserAccountStatusLog(
      Long beApprovalUser, Integer size) {
    return ResultResponse.success(
        userAccountClientService.lastApprovalUserAccountStatusLog(beApprovalUser, size));
  }

  @Override
  public ResultResponse<Void> resetPassword(ResetPasswordCmd cmd) {
    userAccountClientService.resetPassword(cmd);
    return ResultResponse.success();
  }

}
