package com.iqser.red.service.persistence.service.v1.api.shared.model.annotations;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.entitymapped.BaseAnnotation;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.entitymapped.IdRemoval;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.entitymapped.ManualForceRedaction;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.entitymapped.ManualLegalBasisChange;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.entitymapped.ManualRecategorization;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.entitymapped.ManualRedactionEntry;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.entitymapped.ManualResizeRedaction;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ManualRedactions {

    @NonNull
    @Builder.Default
    private Set<IdRemoval> idsToRemove = new HashSet<>();
    @NonNull
    @Builder.Default
    private Set<ManualRedactionEntry> entriesToAdd = new HashSet<>();
    @NonNull
    @Builder.Default
    private Set<ManualForceRedaction> forceRedactions = new HashSet<>();
    @NonNull
    @Builder.Default
    private Set<ManualRecategorization> recategorizations = new HashSet<>();
    @NonNull
    @Builder.Default
    private Set<ManualLegalBasisChange> legalBasisChanges = new HashSet<>();
    @NonNull
    @Builder.Default
    private Set<ManualResizeRedaction> resizeRedactions = new HashSet<>();


    public List<BaseAnnotation> buildAll() {

        List<BaseAnnotation> all = new ArrayList<>(idsToRemove.size()
                                                   + entriesToAdd.size()
                                                   + forceRedactions.size()
                                                   + recategorizations.size()
                                                   + legalBasisChanges.size()
                                                   + resizeRedactions.size());
        all.addAll(idsToRemove);
        all.addAll(entriesToAdd);
        all.addAll(forceRedactions);
        all.addAll(recategorizations);
        all.addAll(legalBasisChanges);
        all.addAll(resizeRedactions);
        return all;
    }


    public List<BaseAnnotation> buildAllSorted() {

        return buildAll().stream()
                .sorted(Comparator.comparing(BaseAnnotation::getRequestDate))
                .toList();
    }


    public boolean isEmpty() {

        return idsToRemove.isEmpty()
               && entriesToAdd.isEmpty()
               && forceRedactions.isEmpty()
               && recategorizations.isEmpty()
               && legalBasisChanges.isEmpty()
               && resizeRedactions.isEmpty();
    }

}
