package com.iqser.red.persistence.service.v1.external.api.impl.controller;

import static com.iqser.red.service.persistence.management.v1.processor.roles.ActionRoles.READ_DOSSIER;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.iqser.red.service.persistence.management.v1.processor.service.DossierStatsService;
import com.iqser.red.service.persistence.service.v1.api.external.resource.DossierStatsResource;
import com.iqser.red.service.persistence.service.v1.api.shared.model.DossierStats;

import feign.FeignException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequiredArgsConstructor
public class DossierStatsController implements DossierStatsResource {

    private final DossierStatsService dossierStatsService;


    @Override
    @PreAuthorize("hasAuthority('" + READ_DOSSIER + "') && hasPermission(#dossierId, 'Dossier', 'VIEW_OBJECT')")
    public DossierStats getDossierStats(@PathVariable(DOSSIER_ID_PARAM) String dossierId) {

        return dossierStatsService.getDossierStats(dossierId);
    }


    @Override
    @PreAuthorize("hasAuthority('" + READ_DOSSIER + "')")
    @PreFilter("hasPermission(filterObject, 'Dossier', 'VIEW_OBJECT')")
    public List<DossierStats> getDossierStats(@RequestBody Set<String> dossierIds) {

        return dossierIds.stream().map(dossierStatsService::getDossierStats).collect(Collectors.toList());
    }

}
