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

import com.red.circle.other.app.dto.clientobject.GrabRoomRedPacketResultCO;
import com.red.circle.other.app.dto.clientobject.RoomRedPacketCO;
import com.red.circle.other.app.dto.clientobject.RoomRedPacketDetailCO;
import com.red.circle.other.app.dto.clientobject.RoomRedPacketRecordCO;
import com.red.circle.other.domain.redpacket.RoomRedPacket;
import com.red.circle.other.domain.redpacket.RoomRedPacketRecord;
import com.red.circle.other.domain.redpacket.RoomRedPacketStatus;
import com.red.circle.other.domain.redpacket.RoomRedPacketType;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 房间红包转换器
 */
public class RoomRedPacketAppConvertor {

    /**
     * Domain转CO
     */
    public static RoomRedPacketCO toCO(RoomRedPacket domain) {
        if (domain == null) {
            return null;
        }

        RoomRedPacketCO co = new RoomRedPacketCO();
        co.setPacketId(domain.getPacketId());
        co.setRoomId(String.valueOf(domain.getRoomId()));
        co.setUserId(domain.getUserId());
        co.setPacketType(domain.getPacketType());
        co.setPacketTypeDesc(getPacketTypeDesc(domain.getPacketType()));
        co.setSourceType(domain.getSourceType());
        co.setTotalAmount(domain.getTotalAmount());
        co.setTotalCount(domain.getTotalCount());
        co.setRemainAmount(domain.getRemainAmount());
        co.setRemainCount(domain.getRemainCount());
        co.setExpireMinutes(domain.getExpireMinutes());
        co.setExpireTime(domain.getExpireTime().toString());
        co.setStatus(domain.getStatus());
        co.setStatusDesc(getStatusDesc(domain.getStatus()));
        co.setCreateTime(domain.getCreateTime().toString());
        
        // 计算倒计时（秒）
        if (domain.getExpireTime() != null) {
            long countdown = Duration.between(LocalDateTime.now(), domain.getExpireTime()).getSeconds();
            co.setCountdown(Math.max(0, countdown));
        }
        
        return co;
    }

    /**
     * Domain转DetailCO
     */
    public static RoomRedPacketDetailCO toDetailCO(RoomRedPacket domain, List<RoomRedPacketRecord> records) {
        if (domain == null) {
            return null;
        }

        RoomRedPacketDetailCO co = new RoomRedPacketDetailCO();
        co.setPacketId(domain.getPacketId());
        co.setUserId(domain.getUserId());
        co.setPacketType(domain.getPacketType());
        co.setPacketTypeDesc(getPacketTypeDesc(domain.getPacketType()));
        co.setTotalAmount(domain.getTotalAmount());
        co.setTotalCount(domain.getTotalCount());
        co.setRemainAmount(domain.getRemainAmount());
        co.setRemainCount(domain.getRemainCount());
        co.setStatus(domain.getStatus());
        co.setStatusDesc(getStatusDesc(domain.getStatus()));
        co.setCreateTime(domain.getCreateTime().toString());
        
        // 转换领取记录
        if (records != null) {
            List<RoomRedPacketRecordCO> recordCOs = records.stream()
                    .map(RoomRedPacketAppConvertor::toRecordCO)
                    .toList();
            co.setRecords(recordCOs);
        }
        
        return co;
    }

    /**
     * Record Domain转CO
     */
    public static RoomRedPacketRecordCO toRecordCO(RoomRedPacketRecord domain) {
        if (domain == null) {
            return null;
        }

        RoomRedPacketRecordCO co = new RoomRedPacketRecordCO();
        co.setRecordId(domain.getRecordId());
        co.setUserId(domain.getUserId());
        co.setAmount(domain.getAmount());
        co.setGrabTime(domain.getGrabTime().toString());
        return co;
    }

    /**
     * 构建抢红包结果CO
     */
    public static GrabRoomRedPacketResultCO toGrabResultCO(String packetId, Long amount, 
                                                            Integer remainCount, Integer totalCount) {
        GrabRoomRedPacketResultCO co = new GrabRoomRedPacketResultCO();
        co.setPacketId(packetId);
        co.setAmount(amount);
        co.setGrabTime(LocalDateTime.now().toString());
        co.setRemainCount(remainCount);
        co.setTotalCount(totalCount);
        return co;
    }

    /**
     * 获取红包类型描述
     */
    private static String getPacketTypeDesc(Integer packetType) {
        RoomRedPacketType type = RoomRedPacketType.of(packetType);
        return type != null ? type.getDesc() : "";
    }

    /**
     * 获取状态描述
     */
    private static String getStatusDesc(Integer status) {
        RoomRedPacketStatus statusEnum = RoomRedPacketStatus.of(status);
        return statusEnum != null ? statusEnum.getDesc() : "";
    }
}
