package com.red.circle.other.app.command.user;

import com.google.common.collect.Sets;
import com.red.circle.external.inner.endpoint.message.ImMessageClient;
import com.red.circle.other.app.dto.cmd.user.relation.cp.CpApplyDismissCmd;
import com.red.circle.other.app.service.user.user.UserProfileService;
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.infra.common.user.UserGiftBackpackCommon;
import com.red.circle.other.infra.database.cache.service.user.UserCpValueCacheService;
import com.red.circle.other.infra.database.mongo.service.user.count.WeekCpValueCountService;
import com.red.circle.other.infra.database.rds.entity.user.user.CpRelationship;
import com.red.circle.other.infra.database.rds.service.user.user.CpApplyService;
import com.red.circle.other.infra.database.rds.service.user.user.CpBlessRecordService;
import com.red.circle.other.infra.database.rds.service.user.user.CpCabinAchieveService;
import com.red.circle.other.infra.database.rds.service.user.user.CpRelationshipService;
import com.red.circle.other.infra.database.rds.service.user.user.CpValueService;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 取消CP申请.
 *
 * @author pengliang on 2021/7/14
 */
@Component
@RequiredArgsConstructor
public class DismissCpApplyCmdExe {

  private final CpValueService cpValueService;
  private final CpApplyService cpApplyService;
  private final ImMessageClient imMessageClient;
  private final CpRelationshipService cpRelationshipService;
  private final UserGiftBackpackCommon userGiftBackpackCommon;
  private final WeekCpValueCountService weekCpValueCountService;
  private final UserCpValueCacheService userCpValueCacheService;
  private final CpBlessRecordService cpBlessRecordService;
  private final CpCabinAchieveService cpCabinAchieveService;
  private final UserProfileGateway userProfileGateway;

  public void execute(CpApplyDismissCmd cmd) {

    var cpRelationship = cpRelationshipService.getByUserId(cmd.requiredReqUserId());
    if (Objects.isNull(cpRelationship)) {
      return;
    }

    // 更新申请状态为解散
    cpApplyService.dismiss(cmd.requiredReqUserId());

    // 更新CP关系为分手中，不删除数据
    cpRelationshipService.updateToDismissing(cmd.requiredReqUserId(), cmd.getCpUserId());

    userProfileGateway.removeCacheAll(Arrays.asList(cmd.requiredReqUserId(), cmd.getCpUserId()));

    // 以下数据不删除，7天后由定时任务清理
    // weekCpValueCountService.removeThisWeek(...)
    // cpValueService.deleteById(...)
    // userGiftBackpackCommon.removeUserCpGift(...)
    // userCpValueCacheService.remove(...)
    // cpCabinAchieveService.deleteByCpValId(...)
    // cpBlessRecordService.deleteByCpValId(...)

  }

  private String getSendApplyUserId(CpApplyDismissCmd cmd) {
    return cmd.requiredReqUserId().toString();
  }

}
