package com.red.circle.other.infra.database.rds.service.game.impl;

import com.red.circle.other.infra.database.rds.dao.game.GameVipLevelWeeklyDAO;
import com.red.circle.other.infra.database.rds.dto.game.WeeklyVipInfo;
import com.red.circle.other.infra.database.rds.entity.game.GameVipLevelWeekly;
import com.red.circle.other.infra.database.rds.service.game.GameVipLevelWeeklyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDate;

@Slf4j
@Service
@RequiredArgsConstructor
public class GameVipLevelWeeklyServiceImpl implements GameVipLevelWeeklyService {

    private final GameVipLevelWeeklyDAO gameVipLevelWeeklyDAO;

    @Override
    public Integer getLastWeekFinalLevel(Long userId, Long activityId, LocalDate currentWeekStartDate) {
        GameVipLevelWeekly lastWeek = gameVipLevelWeeklyDAO.getLastWeekLevel(userId, activityId, currentWeekStartDate);
        return lastWeek != null ? lastWeek.getFinalLevel() : 0;
    }

    @Override
    public WeeklyVipInfo getLastWeekInfo(Long userId, Long activityId, LocalDate currentWeekStartDate) {
        GameVipLevelWeekly lastWeek = gameVipLevelWeeklyDAO.getLastWeekLevel(userId, activityId, currentWeekStartDate);
        if (lastWeek == null) {
            return null;
        }
        
        WeeklyVipInfo info = new WeeklyVipInfo();
        info.setFinalLevel(lastWeek.getFinalLevel());
        info.setRechargeAmount(lastWeek.getRechargeAmount());
        info.setNormalizedWeek(lastWeek.getNormalizedWeek());
        info.setInheritedAmount(lastWeek.getInheritedAmount());
        return info;
    }

    @Override
    public void saveOrUpdateWeeklyLevel(Long userId, Long activityId, Integer weekNumber,
                                         LocalDate weekStartDate, LocalDate weekEndDate,
                                         Integer finalLevel, BigDecimal rechargeAmount, 
                                         Integer normalizedWeek, BigDecimal inheritedAmount) {
        GameVipLevelWeekly existing = gameVipLevelWeeklyDAO.getByUserAndWeek(userId, activityId, weekStartDate);

        if (existing != null) {
            existing.setFinalLevel(finalLevel);
            existing.setRechargeAmount(rechargeAmount);
            existing.setNormalizedWeek(normalizedWeek);
            existing.setInheritedAmount(inheritedAmount);
            existing.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
            gameVipLevelWeeklyDAO.updateById(existing);
        } else {
            GameVipLevelWeekly record = new GameVipLevelWeekly();
            record.setUserId(userId);
            record.setActivityId(activityId);
            record.setWeekNumber(weekNumber);
            record.setWeekStartDate(weekStartDate);
            record.setWeekEndDate(weekEndDate);
            record.setFinalLevel(finalLevel);
            record.setRechargeAmount(rechargeAmount);
            record.setNormalizedWeek(normalizedWeek);
            record.setInheritedAmount(inheritedAmount);
            Timestamp now = new Timestamp(System.currentTimeMillis());
            record.setCreatedAt(now);
            record.setUpdatedAt(now);
            gameVipLevelWeeklyDAO.insert(record);
        }
    }
}
