package com.jasamedika.medifirst2000.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.entities.SkoringTindakanNakes;

@Repository("skoringTindakanNakesDao")
public interface SkoringTindakanNakesDao extends PagingAndSortingRepository<SkoringTindakanNakes, String> {

	String strAllEnabled = "select new Map(stn.noRec as noRec," + "stn.statusEnabled as statusEnabled,"
			+ "stn.skor as skor," + "stn.statusVerifikasi as kdVerif,"
			+ "(case when stn.statusVerifikasi is false then 'Belum Verifikasi' "
			+ "when stn.statusVerifikasi is true then 'Terverifikasi' end) as stVerif,"
			+ "stn.tanggalMulaiBerlaku as tglMulaiBerlaku," + "stn.tanggalPembaharuanData as tglPembaharuanData,"
			+ "prn.id as produkId," + "prn.namaProduk as namaProduk,"
			+ "(case when prn.kdProduk = 1 then 'Pelayanan Pasien' "
			+ "when prn.kdProduk = 2 then 'Nonpelayanan Pasien' end) as jenisProduk," + "pfs.id as profesiId,"
			+ "(case when pfs.jenisProfesi = 1 then concat(pfs.namaProfesi, ' (PPA)') "
			+ "when pfs.jenisProfesi = 2 then concat(pfs.namaProfesi, ' (Non-PPA)') end) as namaProfesi) "
			+ "from SkoringTindakanNakes stn " + "inner join stn.produk prn " + "inner join prn.profesi pfs "
			+ "where stn.statusEnabled is true "
			+ "and to_char(stn.tanggalMulaiBerlaku,'yyyy-MM-dd') <= to_char(now(),'yyyy-MM-dd') ";

	String produk = "and lower(prn.namaProduk) like %:namaProduk% ";

	String statusVerifikasi = "and stn.statusVerifikasi = :isVerif ";

	String profesi = "and pfs.id = :profesiId ";

	String sortSearch = "order by pfs.namaProfesi asc, " + "prn.namaProduk asc, "
			+ "stn.tanggalMulaiBerlaku desc, stn.tanggalPembaharuanData desc";

	@Query(strAllEnabled + sortSearch)
	List<Map<String, Object>> findAllEnabled();

	@Query(strAllEnabled + produk + sortSearch)
	List<Map<String, Object>> findAllEnabledByProduk(@Param("namaProduk") String namaProduk);

	@Query(strAllEnabled + statusVerifikasi + sortSearch)
	List<Map<String, Object>> findAllEnabledByVerif(@Param("isVerif") Boolean isVerif);

	@Query(strAllEnabled + profesi + sortSearch)
	List<Map<String, Object>> findAllEnabledByProfesi(@Param("profesiId") Integer idProfesi);

	@Query(strAllEnabled + produk + statusVerifikasi + sortSearch)
	List<Map<String, Object>> findAllEnabledByProdukVerif(@Param("namaProduk") String namaProduk,
			@Param("isVerif") Boolean isVerif);

	@Query(strAllEnabled + produk + profesi + sortSearch)
	List<Map<String, Object>> findAllEnabledByProdukProfesi(@Param("namaProduk") String namaProduk,
			@Param("profesiId") Integer idProfesi);

	@Query(strAllEnabled + statusVerifikasi + profesi + sortSearch)
	List<Map<String, Object>> findAllEnabledByVerifProfesi(@Param("isVerif") Boolean isVerif,
			@Param("profesiId") Integer idProfesi);

	@Query(strAllEnabled + produk + statusVerifikasi + profesi + sortSearch)
	List<Map<String, Object>> findAllEnabledByProdukVerifProfesi(@Param("namaProduk") String namaProduk,
			@Param("isVerif") Boolean isVerif, @Param("profesiId") Integer idProfesi);

	String strCekDupSkoring = "select stn.noRec " + "from SkoringTindakanNakes stn " + "inner join stn.produk prn "
			+ "inner join prn.profesi pfs " + "where stn.statusEnabled is true "
			+ "and lower(regexp_replace(prn.namaProduk,:patternText,'','g')) = :namaProduk "
			+ "and pfs.id = :profesiId ";

	String skor = " and stn.skor = :skor";

	String tglBerlaku = " and to_char(stn.tanggalMulaiBerlaku,'yyyy-MM-dd') = :tglMulaiBerlaku";

	String idSkoring = " and stn.noRec <> :noRec";

	String sortTglBerlaku = " order by stn.tanggalMulaiBerlaku desc";

	@Query(strCekDupSkoring + skor + sortTglBerlaku)
	List<String> findLastDupSkoring(@Param("namaProduk") String namaProduk, @Param("profesiId") Integer idProfesi,
			@Param("skor") Double skor, @Param("patternText") String patternText);

	@Query(strCekDupSkoring + tglBerlaku)
	List<String> findDupSkoring(@Param("namaProduk") String namaProduk, @Param("profesiId") Integer idProfesi,
			@Param("tglMulaiBerlaku") String tglMulaiBerlaku, @Param("patternText") String patternText);

	@Query(strCekDupSkoring + skor + idSkoring + sortTglBerlaku)
	List<String> findLastDupSkoring(@Param("namaProduk") String namaProduk, @Param("profesiId") Integer idProfesi,
			@Param("skor") Double skor, @Param("patternText") String patternText, @Param("noRec") String noRec);

	@Query(strCekDupSkoring + tglBerlaku + idSkoring)
	List<String> findDupSkoring(@Param("namaProduk") String namaProduk, @Param("profesiId") Integer idProfesi,
			@Param("tglMulaiBerlaku") String tglMulaiBerlaku, @Param("patternText") String patternText,
			@Param("noRec") String noRec);

	@Query("select new Map(stn.noRec as noRec,stn.skor as skor," + "prn.namaProduk as namaProduk,"
			+ "pfs.namaProfesi as namaProfesi) " + "from SkoringTindakanNakes stn " + "inner join stn.produk prn "
			+ "inner join prn.profesi pfs " + "where stn.statusEnabled is true " + "and stn.noRec in (:listNorec)")
	List<Map<String, Object>> findAllDup(@Param("listNorec") List<String> listNorec);

	@Query(value = "select * from sdm_skoringtindakannakes_t "
			+ "where statusenabled is true and statusverifikasi is true "
			+ "and objectproduknakesfk = :produkId and to_char(tanggalmulaiberlaku,'yyyy-MM-dd') <= :tglMulaiBerlaku "
			+ "order by tanggalmulaiberlaku desc, tanggalpembaharuandata desc limit 1", nativeQuery = true)
	SkoringTindakanNakes findLast(@Param("produkId") Integer idProduk,
			@Param("tglMulaiBerlaku") String tglMulaiBerlaku);

}
