package com.jasamedika.medifirst2000.dao;

import com.jasamedika.medifirst2000.entities.Alamat;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

/**
 * Repository class for Alamat
 * 
 * @author Askur
 */
public interface AlamatDao extends JpaRepository<Alamat, Integer> {
	@Query("select model from Alamat model where model.pasien.namaPasien=:nama")
	List<Alamat> findAlamatBynamaPasien(@Param("nama") String nama);

	@Query("select model from Alamat model where model.pasien.namaPasien=:nama")
	List<Alamat> findAlamatByNamaNoCm(@Param("nama") String nama);

	@Query("select model from Alamat model where model.pasien.id=:id")
	Alamat findAlamatByPasienId(@Param("id") Integer id);

	@Query("select model from Alamat model where model.pasien.namaPasien=:nama")
	Page<Alamat> findPageAlamatBynamaPasien(@Param("nama") String nama, Pageable Page);

	@Query("select new map(model.id as id, model.pasien.namaPasien as nama) from Alamat model")
	List<Map<String, String>> getMapAlamat();

	@Query(value = "SELECT " + "alamatlengkap AS NAMA " + "FROM Alamat", nativeQuery = true)
	List<Object[]> getNativeAlamat();

	@Query("select p from Alamat p left join fetch p.desaKelurahan d where p.pasienId=:id")
	List<Alamat> findAlamatByIdPasien(@Param("id") Integer id);

	@Query("select new Map(a.alamatLengkap as alamat," + "p.namaPropinsi as namaprop,"
			+ "b.namaKotaKabupaten as namadati2," + "c.namaKecamatan as namakec," + "d.namaDesaKelurahan as namakel) "
			+ "from Alamat a " + "left join a.propinsi p " + "left join a.kotaKabupaten b " + "left join a.kecamatan c "
			+ "left join a.desaKelurahan d " + "where a.statusEnabled is true " + "and a.pasien.noCm = :noCm")
	List<Map<String, Object>> findAlamatByNoRM(@Param("noCm") String noCm);

	@Query("select new Map(a.alamatLengkap as alamatLengkap, " + "a.rtrw as rtrw, "
			+ "a.namaDesaKelurahan as desaKelurahan, " + "a.namaKecamatan as kecamatan, "
			+ "a.namaKotaKabupaten as kotaKabupaten, " + "p.namaPropinsi as namaPropinsi) " + "from Alamat a "
			+ "left join a.desaKelurahan dk " + "left join a.propinsi p " + "where a.pasien.id=:patientId")
	Map<String, Object> findFullAddressByPatientId(@Param("patientId") Integer pasienId);

	@Query("select alm from Alamat alm where alm.pasienId in (:listIdPasien)")
	List<Alamat> findByPasienIdIn(@Param("listIdPasien") List<Integer> listIdPasien);
}
