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

public interface LegalBasisChangeRepository extends JpaRepository<ManualLegalBasisChangeEntity, AnnotationEntityId>, AnnotationEntityRepository {

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


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


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


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


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


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

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


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

}
