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

import com.iqser.red.service.persistence.management.v1.processor.entity.configuration.DictionaryFalsePositiveEntryEntity;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface FalsePositiveEntryRepository extends FalsePositiveEntryRepositoryCustom, JpaRepository<DictionaryFalsePositiveEntryEntity, Long> {

    @Modifying
    @Query("update DictionaryFalsePositiveEntryEntity e set e.version = :version where e.typeId = :typeId and e.deleted = false")
    void updateVersionWhereTypeId(long version, String typeId);


    List<DictionaryFalsePositiveEntryEntity> findByTypeIdAndVersionGreaterThan(String typeId, long version);


    @Modifying
    @Transactional
    @Query("update DictionaryFalsePositiveEntryEntity e set e.deleted = true, e.version = :version where e.typeId = :typeId")
    void deleteAllEntriesForTypeId(String typeId, long version);


    @Modifying(flushAutomatically = true, clearAutomatically = true)
    @Transactional
    @Query(value = "insert into dictionary_false_positive_entry (value, version, deleted, type_id) " + " select value, 1, false, :newTypeId from dictionary_false_positive_entry where type_id = :originalTypeId and deleted = false", nativeQuery = true)
    void cloneEntries(String originalTypeId, String newTypeId);


    List<DictionaryFalsePositiveEntryEntity> findByTypeIdContainsAndValue(String typeId, String value);

}
