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.IdRemovalEntity;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.AnnotationStatus;

public interface RemoveRedactionRepository extends JpaRepository<IdRemovalEntity, AnnotationEntityId>, AnnotationEntityRepository {

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


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

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


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


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

}
