package cn.yunzhixue.ability.center.examsprint.infrastructure.report.repository;

import cn.yunzhixue.ability.center.examsprint.domain.report.ExamSprintReport;
import cn.yunzhixue.ability.center.examsprint.domain.report.ExamSprintReportRepository;
import org.springframework.stereotype.Repository;

import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Repository
public class InMemoryExamSprintReportRepository implements ExamSprintReportRepository {

    private final ConcurrentMap<String, ExamSprintReport> storage = new ConcurrentHashMap<>();

    @Override
    public ExamSprintReport save(ExamSprintReport report) {
        storage.put(report.reportId(), report);
        return report;
    }

    @Override
    public Optional<ExamSprintReport> findById(String reportId) {
        return Optional.ofNullable(storage.get(reportId));
    }

    @Override
    public List<ExamSprintReport> findExpiredAtOrBefore(Instant instant) {
        return storage.values().stream()
                .filter(report -> report.isExpiredAt(instant))
                .toList();
    }
}
