package es.uv.saic.domain;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import es.uv.saic.dto.OrganDTO;
import es.uv.saic.dto.OrganRepositoryDTO;

@Repository
public interface OrganRepository extends JpaRepository<Organ, OrganPK> {

	@Query("SELECT i FROM Organ i WHERE i.id.tlugar = ?1 AND i.id.lugar = ?2")
	public Organ findByTlugarLugar(String tlugar, Integer lugar);
	
	@Query(value=("SELECT * FROM organs WHERE ruct = ?1 AND actiu"), nativeQuery=true)
	public Organ findByRuct(Integer ruct);

	@Query(value=("SELECT o1.lugar AS lugar, o1.tlugar AS tlugar," + 
				"o1.lugar2 AS lugar2, o1.tlugar2 AS tlugar2, " + 
				"o1.nom_cas AS nomCas, o1.nom_val AS nomVal," + 
				"o1.ruct AS ruct, o1.tambit AS tambit," + 
				"o2.nom_cas AS nomCas2, o2.nom_val AS nomVal2, " +
				"o2.ruct AS ruct2 " + 
				"FROM organs AS o1 JOIN organs AS o2 on o1.tlugar2 = o2.tlugar AND o1.lugar2 = o2.lugar WHERE o1.ruct = ?1 AND o1.actiu = true;"), nativeQuery=true)
	public OrganDTO findByRuctDTO(Integer ruct);

	@Query(value=("""
		SELECT lugar FROM organs WHERE tlugar = ?1 AND lugar = ?2
		UNION 
		SELECT lugar FROM organs_equivalents WHERE tlugar_eq = ?1 AND lugar_eq = ?2; """), nativeQuery=true)
	public List<Integer> exists(String tlugar, Integer lugar);
	
	@Query(value=("SELECT * FROM organs "
			+ "WHERE tlugar = 'C' "
			+ "AND lugar IN(SELECT DISTINCT centre FROM instancies)"), nativeQuery=true)
	public List<Organ> findCurrentCentres();
	
	@Query(value=("SELECT DISTINCT titulacio FROM instancies WHERE centre IN(?1)"), nativeQuery=true)
	public List<Integer> findTitulacionsByCentre(List<Integer> centre);

	@Query(value=("SELECT * FROM organs WHERE actiu = true AND tlugar = 'T' AND tlugar2 = 'C' AND lugar = ?1 ORDER BY nom_val"), nativeQuery=true)
	public Organ findByTitulacio(Integer titulacio);
	
	@Query(value=("SELECT * FROM organs WHERE tlugar = 'T' AND tlugar2 = 'C' AND lugar = ?1 ORDER BY nom_val"), nativeQuery=true)
	public Organ findSupervisableByTitulacio(Integer titulacio);
	
	@Query(value=("SELECT * FROM organs WHERE tlugar = 'T' AND lugar NOT IN (0, 1, 2) AND actiu ORDER BY nom_val"), nativeQuery=true)
	public List<Organ> findAllTitulacions();
	
	@Query(value=("SELECT * FROM organs WHERE tlugar = 'C' ORDER BY nom_val"), nativeQuery=true)
	public List<Organ> findAllCentres();
	
	@Query(value=("SELECT * FROM organs WHERE tlugar = 'C' AND actiu ORDER BY nom_val"), nativeQuery=true)
	public List<Organ> findAllActiveCentres();
	
	@Query(value=("SELECT * FROM organs WHERE tlugar = 'T' AND lugar NOT IN (0, 1, 2) AND actiu ORDER BY nom_val"), nativeQuery=true)
	public List<Organ> findActiveTitulacions();
	
	@Query(value=("SELECT * FROM organs WHERE tlugar = 'C' AND actiu ORDER BY nom_val"), nativeQuery=true)
	public List<Organ> findActiveCentres();
	
	@Query(value=("SELECT * FROM organs WHERE tlugar = 'T' AND lugar NOT IN (0, 1, 2) AND tlugar2 = 'C' AND lugar2 = ?1 AND actiu ORDER BY tambit, nom_val"), nativeQuery=true)
	public List<Organ> findActiveTitulacionsByCentre(Integer centre);	
	
	@Query(value=("SELECT * FROM organs WHERE tlugar = 'T' AND lugar NOT IN (0, 1, 2) AND tlugar2 = 'C' AND lugar2 = ?1 AND CAST(lugar/1000 AS integer) = ?2 AND actiu ORDER BY nom_val"), nativeQuery=true)
	public List<Organ> findActiveTitulacionsByTypeCentre(Integer centre, Integer type);	
	
	@Query(value=("SELECT * FROM organs WHERE tlugar = 'T' AND lugar NOT IN (0, 1, 2) AND tlugar2 = 'C' AND lugar2 = ?1 AND tambit = ?2 AND actiu ORDER BY nom_val"), nativeQuery=true)
	public List<Organ> findActiveTitulacionsByCentreTambit(Integer centre, String tambit);
	
	@Query(value=("SELECT * FROM organs WHERE tlugar = 'T' AND lugar NOT IN (0, 1, 2) AND tlugar2 = 'C' AND lugar2 IN(?1) AND actiu ORDER BY nom_val"), nativeQuery=true)
	public List<Organ> findAllTitulacionsByCentres(List<Integer> centres);
	
	@Query(value=("SELECT * FROM organs WHERE tlugar = 'T' AND lugar IN(SELECT lugar FROM usuaris_rols WHERE usuari = ?1 AND tlugar = 'T') AND actiu ORDER BY nom_val"), nativeQuery=true)
	public List<Organ> findTitulacionsByUsuari(String usuari);
	
	@Query(value=("SELECT DISTINCT * FROM organs WHERE lugar IN(SELECT lugar FROM usuaris_rols WHERE usuari = ?1 AND tlugar = 'C') " + 
			"OR lugar IN(SELECT org.lugar2 FROM organs org, usuaris_rols usr WHERE usr.usuari = ?1 AND usr.tlugar = 'T' AND org.lugar = usr.lugar) " + 
			"AND actiu ORDER BY nom_val"), nativeQuery=true)
	public List<Organ> findCentresByUsuari(String usuari);
	
	@Query(value=("SELECT lugar_eq FROM organs_equivalents WHERE lugar = ?1 AND tlugar = ?2 AND tlugar_eq = ?2 ORDER BY lugar_eq DESC"), nativeQuery=true)
	public List<Integer> getEquivalents(Integer lugar, String tlugar);

	@Query(value=("SELECT o1.lugar AS lugar, o1.tlugar AS tlugar," + 
				"o1.lugar2 AS lugar2, o1.tlugar2 AS tlugar2, " + 
				"o1.nom_cas AS nomCas, o1.nom_val AS nomVal," + 
				"o1.ruct AS ruct, o1.tambit AS tambit," + 
				"o2.nom_cas AS nomCas2, o2.nom_val AS nomVal2, " +
				"o2.ruct AS ruct2 " + 
				"FROM organs AS o1 JOIN organs AS o2 on o1.tlugar2 = o2.tlugar AND o1.lugar2 = o2.lugar WHERE o1.actiu = true;"), nativeQuery=true)
	public List<OrganRepositoryDTO> findAllTitulacionsWithCentre();
	
}
