package es.uv.saic.domain;

import java.util.List;

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 org.springframework.transaction.annotation.Transactional;

import es.uv.saic.dto.IndicadorEnquestaTmpDup;

@Repository
public interface IndicadorEnquestaTmpRepository extends JpaRepository<IndicadorEnquestaTmp, Long> {
	
	@Query("SELECT i FROM IndicadorEnquestaTmp i")
	List<IndicadorEnquestaTmp> findAll();
	
	@Query(value="SELECT * FROM indicador_enquesta_tmp WHERE enquesta = ?1 AND curs = ?2 AND ambit = ?3 AND estudi = ?4", nativeQuery=true)
	List<IndicadorEnquestaTmp> findByEnquestaCursAmbitEstudi(String enquesta, Integer curs, String ambit, String estudi);
	
	@Transactional
	@Modifying(clearAutomatically=true, flushAutomatically=true)
	@Query(value="DELETE FROM indicador_enquesta_tmp WHERE enquesta = ?1", nativeQuery=true)
	int deleteByEnquesta(String enquesta);
	
	@Transactional
	@Modifying(clearAutomatically=true, flushAutomatically=true)
	@Query(value="DELETE FROM indicador_enquesta_tmp WHERE enquesta = ?1 AND curs = ?2 AND ambit = ?3 AND estudi = ?4", nativeQuery=true)
	int deleteByEnquestaCursAmbitEstudi(String enquesta, Integer curs, String ambit, String estudi);
	
	@Query(value="SELECT curs, enquesta, indicador, titulacio, centre, ambit, tipus, estudi, cursd FROM indicador_enquesta_tmp "
			+ "WHERE enquesta = ?1 "
			+ "GROUP BY curs, enquesta, indicador, titulacio, centre, ambit, tipus, estudi, cursd "
			+ "HAVING COUNT(*) > 1", nativeQuery=true)
	List<IndicadorEnquestaTmpDup> checkDupicates(String enquesta);
	
	@Query(value="SELECT COUNT(*) FROM indicador_enquesta_tmp WHERE enquesta = ?1", nativeQuery=true)
	Integer countByEnquesta(String enquesta);
	
	@Query(value="SELECT curs, enquesta, indicador, titulacio, centre, ambit, tipus, estudi, cursd "
				+ "FROM indicador_enquesta_tmp WHERE enquesta = ?1 "
				+ "INTERSECT "
				+ "SELECT curs, enquesta, indicador, titulacio, centre, ambit, tipus, estudi, cursd "
				+ "FROM indicador_enquesta WHERE enquesta = ?1", nativeQuery=true)
	List<IndicadorEnquestaTmpDup> checkIntegrity(String enquesta);

	@Query(nativeQuery=true, value="""
		WITH deleted AS (
			DELETE FROM indicador_enquesta
			WHERE (curs, enquesta, indicador, COALESCE(titulacio, -1), COALESCE(centre, -1), ambit, tipus, estudi, COALESCE(cursd, '')) IN(
				SELECT curs, enquesta, indicador, COALESCE(titulacio, -1) AS titulacio, COALESCE(centre, -1) AS centre, ambit, 
					   tipus, estudi, COALESCE(cursd, '') AS cursd 
				FROM indicador_enquesta_tmp WHERE enquesta = ?1
				INTERSECT 
				SELECT curs, enquesta, indicador, COALESCE(titulacio, -1) AS titulacio, COALESCE(centre, -1) AS centre, ambit, 
					   tipus, estudi, COALESCE(cursd, '') AS cursd  
				FROM indicador_enquesta WHERE enquesta = ?1) 
			RETURNING *)
		SELECT COUNT(*) FROM deleted; """)
	Integer deleteFromCurrent(String enquesta);

	@Query(nativeQuery=true, value="""
		WITH deleted AS (
			DELETE FROM indicador_enquesta_tmp
			WHERE (curs, enquesta, indicador, COALESCE(titulacio, -1), COALESCE(centre, -1), ambit, tipus, estudi, COALESCE(cursd, '')) IN(
				SELECT curs, enquesta, indicador, COALESCE(titulacio, -1) AS titulacio, COALESCE(centre, -1) AS centre, ambit, 
					   tipus, estudi, COALESCE(cursd, '') AS cursd 
				FROM indicador_enquesta_tmp WHERE enquesta = ?1
				INTERSECT 
				SELECT curs, enquesta, indicador, COALESCE(titulacio, -1) AS titulacio, COALESCE(centre, -1) AS centre, ambit, 
					   tipus, estudi, COALESCE(cursd, '') AS cursd  
				FROM indicador_enquesta WHERE enquesta = ?1) 
			RETURNING *)
		SELECT COUNT(*) FROM deleted; """)
	Integer deleteFromPending(String enquesta);

	@Query(nativeQuery=true, value="""
		WITH deleted AS (
			DELETE FROM
			indicador_enquesta_tmp AS a
			USING indicador_enquesta_tmp AS b
			WHERE
				a.enquesta = ?1 AND b.enquesta = ?1 AND a.id < b.id
				AND COALESCE(a.curs, -1) = COALESCE(b.curs, -1) AND COALESCE(a.enquesta, '') = COALESCE(b.enquesta, '') 
				AND COALESCE(a.indicador, '') = COALESCE(b.indicador, '') AND COALESCE(a.titulacio, -1) = COALESCE(b.titulacio, -1) 
				AND COALESCE(a.centre, -1) = COALESCE(b.centre, -1) AND COALESCE(a.ambit, '') = COALESCE(b.ambit, '') 
				AND COALESCE(a.tipus, '') = COALESCE(b.tipus, '') AND COALESCE(a.estudi, '') = COALESCE(b.estudi, '') 
				AND COALESCE(a.cursd, '') = COALESCE(b.cursd, '')
			RETURNING *)
		SELECT COUNT(*) FROM deleted; """)
	Integer deleteDuplicates(String enquesta);

}

