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 com.iqser.red.service.persistence.management.v1.processor.entity.annotations.AnnotationEntityId;
import com.iqser.red.service.persistence.management.v1.processor.entity.annotations.ManualRecategorizationEntity;
import com.iqser.red.service.persistence.service.v1.api.shared.model.annotations.AnnotationStatus;

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

    @Modifying(clearAutomatically = true)
    @Query("update ManualRecategorizationEntity mir set mir.status = :annotationStatus " + "where mir.id = :annotationEntityId")
    void updateStatus(AnnotationEntityId annotationEntityId, AnnotationStatus annotationStatus);


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


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


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


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

}
