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

import java.util.List;
import java.util.Set;

import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.stereotype.Service;

import com.iqser.red.service.persistence.service.v1.api.shared.model.IHavingDossierId;
import com.iqser.red.service.persistence.service.v1.api.shared.model.dossiertemplate.dossier.Dossier;

/*
 * Service for executing Spring Security filtering operations
 * Used to filter nested collections (i.e. Dossiers) in objects (i.e. DossierStatus)
 * Mainly used to prevent information leakage
 */
@Service
public class FilterByPermissionsService {

    @PreFilter("hasPermission(filterObject.getDossierId(), 'Dossier', 'VIEW_OBJECT')")
    public <T extends IHavingDossierId> List<T> onlyViewableHavingDossierId(List<T> items) {

        return items;

    }

    @PreFilter("hasPermission(filterObject, 'Dossier', 'VIEW_OBJECT')")
    public Set<String> onlyViewableDossierIds(Set<String> dossierIds) {

        return dossierIds;

    }

    @PreFilter("hasPermission(filterObject, 'Dossier', 'VIEW_OBJECT')")
    public List<String> onlyViewableDossierIds(List<String> dossierIds) {

        return dossierIds;

    }


    @PreFilter("hasPermission(filterObject.dossierId, 'Dossier', 'VIEW_OBJECT')")
    public List<Dossier> onlyViewableDossiers(List<Dossier> dossiers) {

        return dossiers;

    }


    @PreFilter("hasPermission(filterObject, 'Dossier', 'ACCESS_OBJECT')")
    public List<String> onlyAccessibleDossierIds(List<String> dossierIds) {

        return dossierIds;

    }


    @PreFilter("hasPermission(filterObject, 'Dossier', 'ACCESS_OBJECT')")
    public Set<String> onlyAccessibleDossierIds(Set<String> dossierIds) {

        return dossierIds;

    }


    @PreFilter("hasPermission(filterObject, 'Dossier', 'OWNER')")
    public Set<String> onlyOwnedDossierIds(Set<String> dossierIds) {

        return dossierIds;

    }


    @PreFilter("hasPermission(filterObject.dossierId, 'Dossier', 'ACCESS_OBJECT')")
    public List<Dossier> onlyAccessibleDossiers(List<Dossier> dossiers) {

        return dossiers;

    }


    @PreFilter("hasPermission(filterObject, 'Dossier', 'REVIEW')")
    public List<String> onlyReviewableDossierIds(List<String> dossierIds) {

        return dossierIds;

    }


    @PreFilter("hasPermission(filterObject.dossierId, 'Dossier', 'REVIEW')")
    public List<Dossier> onlyReviewableDossiers(List<Dossier> dossiers) {

        return dossiers;

    }


    @PreFilter("hasPermission(filterObject, 'Dossier', 'APPROVE')")
    public List<String> onlyApprovableDossierIds(List<String> dossierIds) {

        return dossierIds;

    }


    @PreFilter("hasPermission(filterObject.dossierId, 'Dossier', 'APPROVE')")
    public List<Dossier> onlyApprovableDossiers(List<Dossier> dossiers) {

        return dossiers;

    }

}
