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

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.common.business.enums.SendPropsOrigin;
import com.red.circle.component.redis.annotation.TaskCacheLock;
import com.red.circle.other.infra.database.rds.dao.activity.UserActivityRechargeDAO;
import com.red.circle.other.infra.database.rds.dto.activity.ActivityRechargeRankDTO;
import com.red.circle.other.inner.model.cmd.activity.SendActivityRewardCmd;
import com.red.circle.other.inner.endpoint.activity.PropsActivityClient;
import com.red.circle.tool.core.collection.CollectionUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 2026 Spring Festival 充值活动奖励发放.
 *
 * @author tf
 */
@Slf4j
//@Component
@RequiredArgsConstructor
public class SpringFestivalRechargeRewardTask {

    private static final Long ACTIVITY_ID = -1L;
    private static final String RECHARGE_TYPE = "LUCKY_WIN";

    // TOP1~TOP3 各自独立奖励组，TOP4~10 共用同一个
    private static final long REWARD_TOP1    = 2021175644056657922L;
    private static final long REWARD_TOP2    = 2021175937418862593L;
    private static final long REWARD_TOP3    = 2021176242588033025L;
    private static final long REWARD_TOP4_10 = 2021176565687853057L;

    private final UserActivityRechargeDAO userActivityRechargeDAO;
    private final PropsActivityClient propsActivityClient;

    /**
     * Asia/Riyadh 周六 0点执行，只执行一次.
     */
//    @Scheduled(cron = "0 0 0 ? * SAT", zone = "Asia/Riyadh")
//    @TaskCacheLock(key = "SPRING_FESTIVAL_RECHARGE_REWARD_TASK", expireSecond = 86400)
    public void sendRechargeReward() {
        log.warn("[SpringFestival] 充值活动奖励发放 start");
        try {
            Page<ActivityRechargeRankDTO> page = new Page<>(1, 10);
            List<ActivityRechargeRankDTO> top10 = userActivityRechargeDAO.pageRechargeRank(page, ACTIVITY_ID, RECHARGE_TYPE)
                    .getRecords();

            if (CollectionUtils.isEmpty(top10)) {
                log.warn("[SpringFestival] 排行榜为空，跳过奖励发放");
                return;
            }

            for (int i = 0; i < top10.size(); i++) {
                int rank = i + 1;
                Long userId = top10.get(i).getUserId();
                long rewardGroupId = resolveRewardGroupId(rank);
                try {
                    propsActivityClient.sendActivityReward(new SendActivityRewardCmd()
                            .setTrackId(ACTIVITY_ID)
                            .setOrigin(SendPropsOrigin.RANKING_ACTIVITY)
                            .setSysOrigin(SysOriginPlatformEnum.LIKEI)
                            .setSourceGroupId(rewardGroupId)
                            .setAcceptUserId(userId)
                    );
                    log.info("[SpringFestival] 奖励发放成功 rank={} userId={} rewardGroupId={}", rank, userId, rewardGroupId);
                } catch (Exception e) {
                    log.error("[SpringFestival] 奖励发放失败 rank={} userId={}", rank, userId, e);
                }
            }
        } catch (Exception e) {
            log.error("[SpringFestival] 充值活动奖励发放异常", e);
        }
        log.warn("[SpringFestival] 充值活动奖励发放 end");
    }

    private long resolveRewardGroupId(int rank) {
        switch (rank) {
            case 1: return REWARD_TOP1;
            case 2: return REWARD_TOP2;
            case 3: return REWARD_TOP3;
            default: return REWARD_TOP4_10;
        }
    }
}
