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

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.iqser.red.service.persistence.management.v1.processor.entity.annotations.AnnotationEntityId;
import com.iqser.red.service.persistence.management.v1.processor.entity.annotations.ManualForceRedactionEntity;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.AnnotationStatus;

public interface ForceRedactionRepository extends JpaRepository<ManualForceRedactionEntity, AnnotationEntityId>, AnnotationEntityRepository {

    @Modifying(clearAutomatically = true)
    @Query("update ManualForceRedactionEntity mfr set mfr.status = :annotationStatus " + "where mfr.id = :annotationEntityId")
    void updateStatus(AnnotationEntityId annotationEntityId, AnnotationStatus annotationStatus);


    @Modifying
    @Query("update ManualForceRedactionEntity mfr set mfr.softDeletedTime = :softDeletedTime " + "where mfr.id = :annotationEntityId")
    void updateSoftDelete(AnnotationEntityId annotationEntityId, OffsetDateTime softDeletedTime);


    @Query("select mfr from ManualForceRedactionEntity mfr where mfr.id = :annotationEntityId and mfr.softDeletedTime is null")
    Optional<ManualForceRedactionEntity> findByIdAndNotSoftDeleted(AnnotationEntityId annotationEntityId);


    @Query("select mfr from ManualForceRedactionEntity mfr where mfr.id.fileId = :fileId and (:includeDeletions = true or mfr.softDeletedTime is null)")
    List<ManualForceRedactionEntity> findByFileIdIncludeDeletions(String fileId, boolean includeDeletions);


    @Modifying
    @Query("update ManualForceRedactionEntity mfr set mfr.processedDate = :processedDate where mfr.id = :annotationEntityId")
    void markAsProcessed(AnnotationEntityId annotationEntityId, OffsetDateTime processedDate);

}
