package com.red.circle.other.infra.gateway.lottery.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.infra.database.rds.entity.LotteryBackpackLog;
import com.red.circle.other.inner.model.dto.activity.LotteryBackpackLogCO;
import com.red.circle.other.infra.database.rds.service.lottery.LotteryBackpackLogDatabaseService;
import com.red.circle.other.infra.gateway.lottery.LotteryBackpackGateway;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 抽奖背包网关实现
 */
@Component
@RequiredArgsConstructor
public class LotteryBackpackGatewayImpl implements LotteryBackpackGateway {

    private static final Long FIXED_ACTIVITY_ID = 2004471533988167125L;

    private final LotteryBackpackLogDatabaseService logDatabaseService;

    @Override
    public void recordTicketAdd(Long userId, Integer amount, String source, String bizNo) {
        if (amount == null || amount <= 0) {
            return;
        }

        LotteryBackpackLog log = buildLog(userId, "lottery_ticket", "水晶", 
                "https://xxx.com/crystal.png", 1, amount, source, bizNo);
        logDatabaseService.save(log);
    }

    @Override
    public void recordTicketDeduct(Long userId, Integer amount, String source, String bizNo) {
        if (amount == null || amount <= 0) {
            return;
        }

        LotteryBackpackLog log = buildLog(userId, "lottery_ticket", "水晶", 
                "https://xxx.com/crystal.png", 2, amount, source, bizNo);
        logDatabaseService.save(log);
    }

    @Override
    public void recordFragmentAdd(Long userId, Integer amount, String source, String bizNo) {
        if (amount == null || amount <= 0) {
            return;
        }

        LotteryBackpackLog log = buildLog(userId, "cp_fragment", "碎片", 
                "https://xxx.com/fragment.png", 1, amount, source, bizNo);
        logDatabaseService.save(log);
    }

    @Override
    public void recordFragmentDeduct(Long userId, Integer amount, String source, String bizNo) {
        if (amount == null || amount <= 0) {
            return;
        }

        LotteryBackpackLog log = buildLog(userId, "cp_fragment", "碎片", 
                "https://xxx.com/fragment.png", 2, amount, source, bizNo);
        logDatabaseService.save(log);
    }

    private LotteryBackpackLog buildLog(Long userId, String itemType, String itemName,
                                        String itemIcon, Integer changeType, Integer changeAmount,
                                        String source, String bizNo) {
        LotteryBackpackLog log = new LotteryBackpackLog();
        log.setUserId(userId);
        log.setItemType(itemType);
        log.setItemName(itemName);
        log.setItemIcon(itemIcon);
        log.setChangeType(changeType);
        log.setChangeAmount(changeAmount);
        log.setSource(source);
        log.setSourceId(String.valueOf(FIXED_ACTIVITY_ID));
        log.setBizNo(bizNo);
        log.setCreatedAt(LocalDateTime.now());
        return log;
    }

    @Override
    public PageResult<LotteryBackpackLogCO> queryUserLogs(Long userId, String itemType, Integer changeType,
                                                          String source, Integer pageNum, Integer pageSize) {
        IPage<LotteryBackpackLog> page = logDatabaseService.queryPage(userId, itemType, changeType, source, pageNum, pageSize);

        List<LotteryBackpackLogCO> records = page.getRecords().stream()
                .map(this::convertToCO)
                .collect(Collectors.toList());
        PageResult <LotteryBackpackLogCO> pageResult = new PageResult<>();
        pageResult.setRecords(records);
        pageResult.setTotal(page.getTotal());
        pageResult.setCurrent(page.getCurrent());
        pageResult.setSize(page.getSize());

        return pageResult;
    }

    private LotteryBackpackLogCO convertToCO(LotteryBackpackLog log) {
        LotteryBackpackLogCO co = new LotteryBackpackLogCO();
        co.setId(log.getId());
        co.setItemType(log.getItemType());
        co.setItemName(log.getItemName());
        co.setItemIcon(log.getItemIcon());
        co.setChangeType(log.getChangeType());
        co.setChangeAmount(log.getChangeAmount());
        co.setSource(log.getSource());
        co.setCreatedAt(Timestamp.valueOf(log.getCreatedAt()));
        return co;
    }

}