package com.iqser.red.service.persistence.management.v1.processor.service.persistence.repository;

import java.time.OffsetDateTime;
import java.util.List;

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

public interface CommentRepository extends JpaRepository<CommentEntity, Long> {

    @Query("select e from CommentEntity e where e.fileId = :fileId and e.annotationId = :annotationId and (:includeDeletions = true or e.softDeletedTime is null)")
    List<CommentEntity> findByFileIdAndAnnotationId(@Param("fileId") String fileId,
                                                    @Param("annotationId") String annotationId,
                                                    @Param("includeDeletions") boolean includeDeletions);


    @Query("select e from CommentEntity e where e.fileId = :fileId and e.annotationId = :annotationId and (:includeDeletions = true or e.softDeletedTime is null) ORDER BY e.date")
    List<CommentEntity> findByFileIdAndAnnotationIdOrderByDate(@Param("fileId") String fileId,
                                                               @Param("annotationId") String annotationId,
                                                               @Param("includeDeletions") boolean includeDeletions);


    @Query("select e from CommentEntity e where e.fileId = :fileId and (:includeDeletions = true or e.softDeletedTime is null)")
    List<CommentEntity> findByFileId(@Param("fileId") String fileId, @Param("includeDeletions") boolean includeDeletions);

    boolean existsByFileIdAndSoftDeletedTimeIsNull(String fileId);

    @Modifying
    @Query("update CommentEntity c set c.softDeletedTime = :softDeleteTime where c.id = :id")
    int updateSoftDelete(@Param("id") long id, @Param("softDeleteTime") OffsetDateTime softDeleteTime);

    @Modifying
    @Query("update CommentEntity c set c.softDeletedTime = :softDeleteTime where c.fileId in (:fileIds) and "
           + "(exists (select a from ManualForceRedactionEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime = :softDeleteTime) or "
           + "exists (select a from IdRemovalEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime = :softDeleteTime) or "
           + "exists (select a from ManualRedactionEntryEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime = :softDeleteTime) or "
           + "exists (select a from ManualRecategorizationEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime = :softDeleteTime) or "
           + "exists (select a from ManualResizeRedactionEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime = :softDeleteTime) or "
           + "exists (select a from ManualLegalBasisChangeEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime = :softDeleteTime)) ")
    void softDeleteCommentsByFilesAndAnnotationSoftDeletedTime(@Param("fileIds") List<String> fileIds, @Param("softDeleteTime") OffsetDateTime softDeleteTime);

    @Modifying
    @Query("update CommentEntity c set c.softDeletedTime = :softDeleteTime where c.fileId in (:fileIds) and c.softDeletedTime is null")
    void softDeleteCommentsByFiles(@Param("fileIds") List<String> fileIds, @Param("softDeleteTime") OffsetDateTime softDeleteTime);


    @Modifying
    @Query("delete from CommentEntity c where c.fileId in (:fileIds)")
    void deleteCommentsByFiles(@Param("fileIds") List<String> fileIds);


    @Modifying
    @Query("update CommentEntity c set c.softDeletedTime = null where c.fileId = :fileId and "
           + "(exists (select a from ManualForceRedactionEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime >= :deletionTime) or "
           + "exists (select a from IdRemovalEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime >= :deletionTime) or "
           + "exists (select a from ManualRedactionEntryEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime >= :deletionTime) or "
           + "exists (select a from ManualRecategorizationEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime >= :deletionTime) or "
           + "exists (select a from ManualResizeRedactionEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime >= :deletionTime) or "
           + "exists (select a from ManualLegalBasisChangeEntity a where a.id.annotationId = c.annotationId and a.softDeletedTime >= :deletionTime)) ")
    void undeleteByFileIdAndAnnotationSoftDeletedTime(@Param("fileId") String fileId, @Param("deletionTime") OffsetDateTime deletionTime);


    @Modifying
    @Query("update CommentEntity c set c.softDeletedTime = null where c.fileId = :fileId and c.softDeletedTime >= :deletionTime")
    void undeleteByFileId(@Param("fileId") String fileId, @Param("deletionTime") OffsetDateTime deletionTime);


    @Modifying
    @Query("update CommentEntity c set c.annotationId = :newAnnotationId where c.annotationId = :oldAnnotationId and c.fileId = :fileId")
    int saasMigrationUpdateAnnotationIds(@Param("fileId") String fileId, @Param("oldAnnotationId") String oldAnnotationId, @Param("newAnnotationId") String newAnnotationId);




}
