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.SkoringTindakanFarmakologi;

/**
 * @author salmanoe
 * @since Jun 20, 2022
 * 
 */
@Repository("skoringTindakanFarmakologiDao")
public interface SkoringTindakanFarmakologiDao extends PagingAndSortingRepository<SkoringTindakanFarmakologi, String> {

	String strAllEnabled = "select new Map(stf.noRec as noRec,stf.statusEnabled as statusEnabled,"
			+ "stf.skor as skor,stf.statusVerifikasi as kdVerif,"
			+ "(case when stf.statusVerifikasi is false then 'Belum Verifikasi' "
			+ "when stf.statusVerifikasi is true then 'Terverifikasi' end) as stVerif,"
			+ "stf.tanggalMulaiBerlaku as tglMulaiBerlaku,stf.tanggalPembaharuanData as tglPembaharuanData,"
			+ "prf.id as produkId,prf.namaProduk as namaProduk,"
			+ "(case when prf.kdProduk = 1 then 'Pelayanan Pasien (Tarif)' "
			+ "when prf.kdProduk = 2 then 'Pelayanan Pasien (Nontarif)' "
			+ "when prf.kdProduk = 3 then 'Pelayanan Nonpasien' end) as jenisProduk) "
			+ "from SkoringTindakanFarmakologi stf " + "inner join stf.produk prf " + "where stf.statusEnabled is true "
			+ "and to_char(stf.tanggalMulaiBerlaku,'yyyy-MM-dd') <= to_char(now(),'yyyy-MM-dd') ";

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

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

	String sortSearch = "order by prf.namaProduk asc, stf.tanggalMulaiBerlaku desc, stf.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 + produk + statusVerifikasi + sortSearch)
	List<Map<String, Object>> findAllEnabledByProdukVerif(@Param("namaProduk") String namaProduk,
			@Param("isVerif") Boolean isVerif);

	String strCekDupSkoring = "select stf.noRec from SkoringTindakanFarmakologi stf " + "inner join stf.produk prf "
			+ "where stf.statusEnabled is true "
			+ "and lower(regexp_replace(prf.namaProduk,:patternText,'','g')) = :namaProduk ";

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

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

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

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

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

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

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

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

	@Query("select new Map(stf.noRec as noRec," + "stf.skor as skor,prf.namaProduk as namaProduk) "
			+ "from SkoringTindakanFarmakologi stf " + "inner join stf.produk prf " + "where stf.statusEnabled is true "
			+ "and stf.noRec in (:listNorec)")
	List<Map<String, Object>> findAllDup(@Param("listNorec") List<String> listNorec);

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

}
