package com.iqser.red.service.persistence.management.v1.processor.utils;

import java.util.function.BiConsumer;
import java.util.stream.Collectors;

import org.springframework.stereotype.Component;

import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.DossierEntity;
import com.iqser.red.service.persistence.management.v1.processor.entity.dossier.DossierStatusEntity;
import com.iqser.red.service.persistence.management.v1.processor.service.FilterByPermissionsService;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.DossierStatusInfo;

import lombok.AllArgsConstructor;

@Component
@AllArgsConstructor
public class DossierStatusFilteringMapper implements BiConsumer<DossierStatusEntity, DossierStatusInfo> {

    private final FilterByPermissionsService filterByPermissionsService;


    @Override
    public void accept(DossierStatusEntity dossierStatusEntity, DossierStatusInfo dossierStatusInfo) {

        dossierStatusInfo.setDossierCount(dossierStatusEntity.getDossiers()
                                                  .isEmpty() ? 0 : (long) filterByPermissionsService.onlyViewableDossierIds(dossierStatusEntity.getDossiers()
                                                                                                                                    .stream()
                                                                                                                                    .map(DossierEntity::getId)
                                                                                                                                    .collect(Collectors.toList())).size());

    }

}
