package com.dacrt.SBIABackend.repository;

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

import javax.transaction.Transactional;

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 com.dacrt.SBIABackend.entity.Facilities;
import com.dacrt.SBIABackend.entity.Instrumentriskfactors;
import com.dacrt.SBIABackend.entity.Instruments;

public interface InstrumentriskfactorsRepository extends JpaRepository<Instrumentriskfactors,Integer>,JpaSpecificationExecutor<Instrumentriskfactors>{

	@Transactional
	@Modifying
	@Query(" DELETE FROM Instrumentriskfactors e where e.instrumentid = ?1 ")
	void deleteInstrumentriskfactorsbyid(Integer id);
	
	@Transactional
	@Modifying
	@Query(" DELETE FROM Instrumentriskfactors e where e.instrumentid = ?1 and e.riskfactorid= ?2")
	void deleteInstrumentrosFactores(Integer idIntrumento, Integer idFactor);
	
	@Query(" SELECT count(id) FROM Instrumentriskfactors e where e.instrumentid = ?1 and e.riskfactorid= ?2")
	int existeInstrumentrosFactores(Integer idIntrumento, Integer idFactor);
	
	@Query(" SELECT e FROM Instrumentriskfactors e where e.instrumentid = ?1 and e.riskfactorid= ?2")
	Optional<Instrumentriskfactors> findInstrumeFactores(Integer idIntrumento, Integer idFactor);
	
	
	@Query(" SELECT e FROM Instrumentriskfactors e where e.instrumentid = ?1 and e.riskfactorid= ?2")
	Instrumentriskfactors findInstrumeFactoresObjeto(Integer idIntrumento, Integer idFactor);
	
			
//	@Query(" select e from main.instrumentriskfactors where instrumentid = ?1 and riskfactorid = ?2 and id <> ?3 ")
//	boolean existeriskfactor(Integer idIntrumento, Integer idFactor,Integer id);
	boolean existsByInstrumentidAndRiskfactoridAndIdNot(Integer instrumentid, Integer riskfactorid, Integer id);

	
	List<Instrumentriskfactors> findByInstrumentid(int instrumentId);
	
	Optional<Instrumentriskfactors> findById(int id);
	
	boolean existsByRiskfactorid(int idriskfactor);
}
