package com.red.circle.other.app.scheduler;

import com.google.common.collect.Sets;
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.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 com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 清理7天前分手中的CP数据.
 *
 * @author AI Assistant
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class CleanExpiredDismissingCpTask {

  private final CpRelationshipService cpRelationshipService;
  private final CpValueService cpValueService;
  private final WeekCpValueCountService weekCpValueCountService;
  private final UserGiftBackpackCommon userGiftBackpackCommon;
  private final UserCpValueCacheService userCpValueCacheService;
  private final CpCabinAchieveService cpCabinAchieveService;
  private final CpBlessRecordService cpBlessRecordService;

  /**
   * 每天凌晨3点执行清理任务.
   */
  @Scheduled(cron = "0 0 3 * * ?")
  public void execute() {
    try {
      List<CpRelationship> expiredList = cpRelationshipService.listExpiredDismissingCp();
      
      if (CollectionUtils.isEmpty(expiredList)) {
        return;
      }

      for (CpRelationship cpRelationship : expiredList) {
        cleanCpData(cpRelationship);
      }

    } catch (Exception e) {
      log.error("CleanExpiredDismissingCpTask execute error", e);
    }
  }

  private void cleanCpData(CpRelationship cpRelationship) {
    try {
      // 删除CP关系
      cpRelationshipService.deleteCpRelationship(cpRelationship.getUserId());

      // 删除周统计
      weekCpValueCountService.removeThisWeek(cpRelationship.getUserId(),
          cpRelationship.getCpUserId());

      // 删除CP值
      cpValueService.deleteById(cpRelationship.getCpValId());

      // 移除CP礼物
      userGiftBackpackCommon.removeUserCpGift(
          Sets.newHashSet(cpRelationship.getUserId(), cpRelationship.getCpUserId()));

      // 清除缓存
      userCpValueCacheService.remove(cpRelationship.getUserId(), cpRelationship.getCpUserId());

      // 删除成就
      cpCabinAchieveService.deleteByCpValId(cpRelationship.getCpValId());

      // 删除祝福记录
      cpBlessRecordService.deleteByCpValId(cpRelationship.getCpValId());

    } catch (Exception e) {
      log.error("CleanExpiredDismissingCpTask cleanCpData error, userId={}, cpUserId={}", 
          cpRelationship.getUserId(), cpRelationship.getCpUserId(), e);
    }
  }

}
