package com.dacrt.SBIABackend.security.repository;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.dacrt.SBIABackend.security.dto.AccessDto;
import com.dacrt.SBIABackend.security.entity.Access;

@Repository
public interface AccessRepository  extends JpaRepository<Access,Integer>{
	

	
	@Query(value=" SELECT * FROM main.access a WHERE a.userid = ?1",nativeQuery=true)
	List<Access> getAllAccessxUser(Integer iduser);
	
	@Query(value="SELECT a.content FROM main.access a WHERE a.userid = ?1 ORDER BY id desc",nativeQuery=true)
	List<String> getAllAccessxUserMax(Integer iduser);
	

	
	//@Query(value="SELECT content from (SELECT a.userid,a.content,max(a.id) as id from main.access a WHERE a.userid = ?1 GROUP BY a.userid,a.content ORDER BY id desc) as t1",nativeQuery=true)
	//boolean existeByContentUser(Integer iduser,String content);
	
	boolean existsByContentAndUserid(String content,Integer iduser);
	
	@Transactional
	@Modifying
	@Query(" DELETE FROM Access e where e.userid = ?1 AND e.content = ?2 ")
	void deleteContentUser(Integer iduser,String content);
	
	
	//select content from (select a.userid,a.content,max(a.id) as id from main.access a where userid = 13 
	//		group by a.userid,a.content order by id desc) as t1
	
	//@Query(value=" SELECT * FROM main.access a WHERE a.userid = ?1 " )
	//Optional<Menu> getAllAccessxUser(Integer idusers);
	
	//select m.dsc from main.menu m,main.access a where userid = 2
	//		and a.content=m.content
	
}