package com.red.circle.other.app.command.task;

import com.red.circle.other.app.dto.clientobject.task.RoomDailyTaskClaimableCountCO;
import com.red.circle.other.infra.database.rds.entity.task.RoomDailyTaskProgress;
import com.red.circle.other.infra.database.rds.service.RoomDailyTaskDatabaseService;
import com.red.circle.tool.core.date.ZonedDateTimeAsiaRiyadhUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * 查询用户当日可领取奖励数量
 */
@Component
@RequiredArgsConstructor
public class RoomDailyTaskClaimableCountQryExe {

    private final RoomDailyTaskDatabaseService taskDatabaseService;

    public RoomDailyTaskClaimableCountCO execute(Long userId) {
        LocalDate today = ZonedDateTimeAsiaRiyadhUtils.now().toLocalDate();
        List<RoomDailyTaskProgress> progressList = taskDatabaseService.listProgressByUser(userId, today);

        int claimableCount = progressList.stream()
                .mapToInt(p -> Math.max(0, p.getMaxTierCompleted() - p.getMaxTierClaimed()))
                .sum();

        RoomDailyTaskClaimableCountCO co = new RoomDailyTaskClaimableCountCO();
        co.setClaimableCount(claimableCount);
        return co;
    }
}
