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

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

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


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


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


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


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


    @Query("""
           select m
           from ManualForceRedactionEntity 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)
           """)
    List<ManualForceRedactionEntity> findByFileIdAndOptions(@Param("fileId") String fileId,
                                                            @Param("includeDeletions") boolean includeDeletions,
                                                            @Param("unprocessed") boolean unprocessed);


    @Query("""
            select m
            from ManualForceRedactionEntity 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 m.id.annotationId in (:annotationIds)         
            """)
    List<ManualForceRedactionEntity> findByFileIdAndOptionsAndAnnotationIds(@Param("fileId") String fileId,
                                                                            @Param("includeDeletions") boolean includeDeletions,
                                                                            @Param("unprocessed") boolean unprocessed,
                                                                            @Param("annotationIds") List<String> annotationIds);


    @Modifying
    @Query("update ManualForceRedactionEntity 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 ManualForceRedactionEntity 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 ManualForceRedactionEntity m where m.id.fileId in (:fileIds)")
    int deleteByFileIds(@Param("fileIds") List<String> fileIds);

}
