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

public interface RecategorizationRepository extends JpaRepository<ManualRecategorizationEntity, AnnotationEntityId>, AnnotationEntityRepository {

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


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


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


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


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


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


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

}
