package com.izouma.nineth.repo;

import com.izouma.nineth.domain.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;

public interface TagRepo extends JpaRepository<Tag, Long>, JpaSpecificationExecutor<Tag> {

    @Query(nativeQuery = true, value = "insert into asset_tag (asset_id, tag_id) " +
            "select id, ?1 from asset where name like ?2")
    @Transactional
    @Modifying
    int addTagForAssetEasy(Long id, String pattern);

    @Query(nativeQuery = true, value = "insert into collection_tag (collection_id, tag_id) " +
            "select id, ?1 from collection_info where name like ?2")
    @Transactional
    @Modifying
    int addTagForCollectionEasy(Long id, String pattern);

    @Query(nativeQuery = true, value = "insert into asset_tag (asset_id, tag_id) " +
            "select id, ?1 from asset where name regexp ?2")
    @Transactional
    @Modifying
    int addTagForAssetRegex(Long id, String pattern);

    @Query(nativeQuery = true, value = "insert into collection_tag (collection_id, tag_id) " +
            "select id, ?1 from collection_info where name regexp ?2")
    @Transactional
    @Modifying
    int addTagForCollectionRegex(Long id, String pattern);

    @Query(nativeQuery = true, value = "select count(id) from asset where name like ?1")
    long countAssetEasy(String pattern);

    @Query(nativeQuery = true, value = "select count(id) from collection_info where name like ?1")
    long countCollectionEasy(String pattern);

    @Query(nativeQuery = true, value = "select count(id) from asset where name regexp ?1")
    long countAssetRegex(String pattern);

    @Query(nativeQuery = true, value = "select count(id) from collection_info where name regexp ?1")
    long countCollectionRegex(String pattern);
}