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

public interface ResizeRedactionRepository extends JpaRepository<ManualResizeRedactionEntity, AnnotationEntityId>, AnnotationEntityRepository {

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


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


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


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


    @Modifying
    @Query("update ManualResizeRedactionEntity m set m.textBefore = :textBefore, m.textAfter = :textAfter where m.id = :id")
    void updateSurroundingText(AnnotationEntityId id, String textBefore, String textAfter);


    List<ManualResizeRedactionEntity> findByStatusAndValue(AnnotationStatus status, String value);


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

}
