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.IdRemovalEntity;

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

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


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


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


    @Query("select idr from IdRemovalEntity idr where idr.id.fileId = :fileId and idr.processedDate is null")
    List<IdRemovalEntity> findByFileIdAndUnprocessed(@Param("fileId") String fileId);


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


    @Query("""
            select m
            from IdRemovalEntity 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.removeFromDictionary = false and m.removeFromAllDossiers = false))
            """)
    List<IdRemovalEntity> findByFileIdAndOptions(@Param("fileId") String fileId,
                                                 @Param("includeDeletions") boolean includeDeletions,
                                                 @Param("unprocessed") boolean unprocessed,
                                                 @Param("includeDictChanges") boolean includeDictChanges);

    @Query("""
            select m
            from IdRemovalEntity 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.removeFromDictionary = false and m.removeFromAllDossiers = false))
            and m.id.annotationId in (:annotationIds)
            """)
    List<IdRemovalEntity> 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 IdRemovalEntity 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 IdRemovalEntity 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 IdRemovalEntity m where m.id.fileId in (:fileIds)")
    int deleteByFileIds(@Param("fileIds") List<String> fileIds);

}
