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;

@Repository
public interface ProcesRepository extends JpaRepository<Proces, Integer> {

	@Query("SELECT i FROM Proces i WHERE i.nomProces = ?1")
	List<Proces> findByName(String nom);
	
	@Query("SELECT i FROM Proces i WHERE i.nomProces = ?1 AND i.cursAvaluat = ?2")
	List<Proces> findByNameCurs(String nom, Integer curs);
	
	@Query(value="SELECT DISTINCT id_proces FROM processos WHERE curs_avaluat IN (?1) AND id_proces IN("
			+ "SELECT id_proces FROM instancies WHERE centre IN(?2) AND titulacio IN(?3) AND nom_proces = ?4)", nativeQuery=true)
	List<Integer> findProcsByCentreTitulacioCursNom(List<Integer> cursos, List<Integer> centres, List<Integer> titulacions, String nom);

	@Query(value="SELECT DISTINCT id_proces FROM processos WHERE curs_avaluat IN (?1) AND id_proces IN("
			+ "SELECT id_proces FROM instancies WHERE centre IN(?2) AND titulacio IN(?3)) ", nativeQuery=true)
	List<Integer> findProcsByCentreTitulacioCurs(List<Integer> cursos, List<Integer> centres, List<Integer> titulacions);
	
	@Query(value="SELECT DISTINCT curs_avaluat FROM processos WHERE id_proces IN("
			+ "SELECT id_proces FROM instancies WHERE centre IN(?1) AND titulacio IN(?2))", nativeQuery=true)
	List<Integer> findCursosByCentreTitulacio(List<Integer> centres, List<Integer> titulacions);

	@Query(nativeQuery=true, value=("""
			SELECT T1.txt FROM(
				SELECT CASE
						WHEN 
							ta.tipus IN(2, 20) 
						THEN 
							CONCAT(ta.id_tascap, '@{shape: hex, label: \"', 
							CASE WHEN LENGTH(ta.titol_cas) > 497 THEN CONCAT(LEFT(ta.titol_cas, 497), '...') ELSE ta.titol_cas END,
							' <br> <strong>Asignado a:</strong> ', ro.descripcio_cas, '<br> <strong>Fecha límite:</strong> ', ta.data_lim, '\"}')
						WHEN
							ta.tipus IN(21, 22) 
						THEN 
							CONCAT(ta.id_tascap, '@{shape: procs, label: \"', 
							CASE WHEN LENGTH(ta.titol_cas) > 497 THEN CONCAT(LEFT(ta.titol_cas, 497), '...') ELSE ta.titol_cas END,
							' <br> <strong>Asignado a:</strong> ', ro.descripcio_cas, '<br> <strong>Fecha límite:</strong> ', ta.data_lim, '\"}')
						WHEN
							ta.tipus IN(1, 11, 12, 14, 15) 
						THEN 
							CONCAT(ta.id_tascap, '@{shape: lean-r, label: \"', 
							CASE WHEN LENGTH(ta.titol_cas) > 497 THEN CONCAT(LEFT(ta.titol_cas, 497), '...') ELSE ta.titol_cas END,
							' <br> <strong>Asignado a:</strong> ', ro.descripcio_cas, '<br> <strong>Fecha límite:</strong> ', ta.data_lim, '\"}')
						ELSE 
							CONCAT(ta.id_tascap, '[\"', 
							CASE WHEN LENGTH(ta.titol_cas) > 497 THEN CONCAT(LEFT(ta.titol_cas, 497), '...') ELSE ta.titol_cas END,
							' <br> <strong>Asignado a:</strong> ', ro.descripcio_cas, '<br> <strong>Fecha límite:</strong> ', ta.data_lim, '\"]')
					END AS txt, ta.id_tascap, 1 AS ordr 
				FROM tasques ta LEFT JOIN rols ro ON ro.id_rol = ta.id_rol 
				WHERE ta.id_proces IN(SELECT id_proces FROM processos WHERE nom_proces = ?1 ORDER BY id_proces DESC LIMIT 1)
				UNION
				SELECT CASE
							WHEN tipus = 2 OR tipus = 20 THEN CONCAT(id_tascap, '-->|SÍ|', id_tasca_seg)
					ELSE 
							CONCAT(id_tascap, '-->', id_tasca_seg) 
					END AS txt, id_tascap, 2 AS ordr 
				FROM tasques 
				WHERE id_proces IN(SELECT id_proces FROM processos WHERE nom_proces = ?1 ORDER BY id_proces DESC LIMIT 1) AND id_tasca_seg != 9999
				UNION
				SELECT CONCAT(id_tascap, '-->|NO|', id_tasca_seg2) AS txt, id_tascap, 2 AS ordr 
				FROM tasques 
				WHERE (tipus = 2 OR tipus = 20) AND id_proces IN(SELECT id_proces FROM processos WHERE nom_proces = ?1 ORDER BY id_proces DESC LIMIT 1) AND id_tasca_seg2 != 9999
				ORDER BY ordr ASC, id_tascap ASC
			) AS T1; """))
	public List<String> getFlowDiagram(String proc);
	
}

