package com.red.circle.other.domain.rocket;

import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 火箭历史领域对象
 *
 * @author system
 * @date 2025-01-15
 */
@Data
public class RocketHistory {

    private String id;
    private Long roomId;
    private String date;
    private Integer level;
    private Long finalEnergy;
    private Long overEnergy;
    private List<HistoryContributor> contributors;
    private Integer totalContributors;
    private RewardPool rewardPool;
    private ClaimStats claimStats;
    /**
     * 已领取人数
     */
    private Integer claimedCount;
    private LocalDateTime launchTime;
    private LocalDateTime claimStartTime;
    private LocalDateTime claimEndTime;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    @Data
    public static class HistoryContributor {
        private Long userId;
        private String userName;
        private String userAvatar;
        private Long energy;
        private BigDecimal contributionRate;
        private Integer rank;
    }

    @Data
    public static class RewardPool {
        private Long totalGold;
        private Long contributorGold;
        private Long normalGold;

        public static RewardPool empty() {
            RewardPool pool = new RewardPool();
            pool.setTotalGold(0L);
            pool.setContributorGold(0L);
            pool.setNormalGold(0L);
            return pool;
        }
    }

    @Data
    public static class ClaimStats {
        private Integer totalClaimed;
        private Integer contributorClaimed;
        private Integer normalClaimed;
        private Long totalRewardSent;

        public static ClaimStats init() {
            ClaimStats stats = new ClaimStats();
            stats.setTotalClaimed(0);
            stats.setContributorClaimed(0);
            stats.setNormalClaimed(0);
            stats.setTotalRewardSent(0L);
            return stats;
        }
    }

    /**
     * 从RocketStatus创建历史记录
     */
    public static RocketHistory fromRocketStatus(RocketStatus status) {
        RocketHistory history = new RocketHistory();
        history.setRoomId(status.getRoomId());
        history.setDate(status.getDate());
        history.setLevel(status.getLevel().getLevel());
        history.setFinalEnergy(status.getCurrentEnergy());

        // 计算超出能量
        Long overEnergy = status.getCurrentEnergy() - status.getMaxEnergy();
        history.setOverEnergy(Math.max(overEnergy, 0L));

        // 转换贡献者
        List<HistoryContributor> historyContributors = new ArrayList<>();
        for (int i = 0; i < Math.min(status.getContributors().size(), 100); i++) {
            RocketContributor contributor = status.getContributors().get(i);
            HistoryContributor hc = new HistoryContributor();
            hc.setUserId(contributor.getUserId());
            hc.setUserName(contributor.getUserName());
            hc.setUserAvatar(contributor.getUserAvatar());
            hc.setEnergy(contributor.getEnergy());
            hc.setContributionRate(contributor.getContributionRate());
            hc.setRank(i + 1);
            historyContributors.add(hc);
        }
        history.setContributors(historyContributors);
        history.setTotalContributors(status.getContributors().size());

        // 设置奖励池 (预留)
        history.setRewardPool(RewardPool.empty());

        // 初始化领取统计
        history.setClaimStats(ClaimStats.init());

        // 设置时间
        history.setLaunchTime(status.getLaunchTime());
        history.setCreatedAt(LocalDateTime.now());
        history.setUpdatedAt(LocalDateTime.now());

        return history;
    }
}
