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 org.springframework.data.repository.query.Param;

import com.iqser.red.service.persistence.management.v1.processor.entity.annotations.AnnotationEntityId;
import com.iqser.red.service.persistence.management.v1.processor.entity.annotations.ManualResizeRedactionEntity;

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

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


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


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


    @Query("select mrd from ManualResizeRedactionEntity mrd where mrd.id.fileId = :fileId and mrd.processedDate is null")
    List<ManualResizeRedactionEntity> findUnprocessedByFileId(@Param("fileId") String fileId);


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


    List<ManualResizeRedactionEntity> findByValue(@Param("value") String value);


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


    @Query("""
            select m
            from ManualResizeRedactionEntity m
            where m.id.fileId = :fileId
            and ((:unprocessed = true and m.processedDate is null) or :unprocessed = false)
            and (:includeDeletions = true or m.softDeletedTime is null)
            and (:includeDictChanges = true or (m.updateDictionary = false and m.addToAllDossiers = false))
            """)
    List<ManualResizeRedactionEntity> findByFileIdAndOptions(@Param("fileId") String fileId,
                                                             @Param("includeDeletions") boolean includeDeletions,
                                                             @Param("unprocessed") boolean unprocessed,
                                                             @Param("includeDictChanges") boolean includeDictChanges);


    @Query("""
            select m
            from ManualResizeRedactionEntity m
            where m.id.fileId = :fileId
            and ((:unprocessed = true and m.processedDate is null) or :unprocessed = false)
            and (:includeDeletions = true or m.softDeletedTime is null)
            and (:includeDictChanges = true or (m.updateDictionary = false and m.addToAllDossiers = false))
            and m.id.annotationId in (:annotationIds)
            """)
    List<ManualResizeRedactionEntity> findByFileIdAndOptionsAndAnnotationIds(@Param("fileId") String fileId,
                                                                             @Param("includeDeletions") boolean includeDeletions,
                                                                             @Param("unprocessed") boolean unprocessed,
                                                                             @Param("includeDictChanges") boolean includeDictChanges,
                                                                             @Param("annotationIds") List<String> annotationIds);


    @Modifying
    @Query("update ManualResizeRedactionEntity m set m.softDeletedTime = :softDeletedTime where m.id.fileId in (:fileIds) and m.softDeletedTime is null")
    int softDeleteByFileIds(@Param("fileIds") List<String> fileIds,
                                      @Param("softDeletedTime") OffsetDateTime softDeletedTime);

    @Modifying
    @Query("update ManualResizeRedactionEntity m set m.softDeletedTime = null where m.id.fileId = :fileId and m.softDeletedTime >= :softDeletedTime")
    int undeleteByFileId(@Param("fileId") String fileId,
                         @Param("softDeletedTime") OffsetDateTime softDeletedTime);

    @Modifying
    @Query("delete ManualResizeRedactionEntity m where m.id.fileId in (:fileIds)")
    int deleteByFileIds(@Param("fileIds") List<String> fileIds);

}
