package org.raddatz.familienarchiv.document.transcription;

import lombok.RequiredArgsConstructor;
import org.raddatz.familienarchiv.document.transcription.TranscriptionBlock;
import org.raddatz.familienarchiv.document.transcription.CompletionStatsRow;
import org.raddatz.familienarchiv.document.transcription.TranscriptionBlockRepository;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class TranscriptionBlockQueryService {

    private final TranscriptionBlockRepository blockRepository;

    public boolean hasBlocks(UUID documentId) {
        return blockRepository.existsByDocumentId(documentId);
    }

    public Map<UUID, Integer> getCompletionStats(List<UUID> documentIds) {
        if (documentIds.isEmpty()) return Map.of();
        Map<UUID, Integer> result = new HashMap<>();
        for (CompletionStatsRow row : blockRepository.findCompletionStatsForDocuments(documentIds)) {
            result.put(row.getDocumentId(), row.getCompletionPercentage());
        }
        return result;
    }

    public List<TranscriptionBlock> findSegmentationBlocks() {
        return blockRepository.findSegmentationBlocks();
    }

    public List<TranscriptionBlock> findEligibleKurrentBlocks() {
        return blockRepository.findEligibleKurrentBlocks();
    }

    public List<TranscriptionBlock> findManualKurrentBlocksByPerson(UUID personId) {
        return blockRepository.findManualKurrentBlocksByPerson(personId);
    }

    public long countManualKurrentBlocksByPerson(UUID personId) {
        return blockRepository.countManualKurrentBlocksByPerson(personId);
    }

    public long count() {
        return blockRepository.count();
    }
}
