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

import com.iqser.red.service.persistence.management.v1.processor.entity.configuration.DictionaryFalseRecommendationEntryEntity;
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 FalseRecommendationEntryRepository extends FalseRecommendationEntryRepositoryCustom, JpaRepository<DictionaryFalseRecommendationEntryEntity, Long> {

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


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


    @Modifying
    @Transactional
    @Query("update DictionaryFalseRecommendationEntryEntity 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_recommendation_entry (value, version, deleted, type_id) " + " select value, 1, false, :newTypeId from dictionary_false_recommendation_entry where type_id = :originalTypeId and deleted = false", nativeQuery = true)
    void cloneEntries(String originalTypeId, String newTypeId);


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

}
