package org.ros.chatto.repository;

import java.util.List;
import java.util.Optional;

import org.ros.chatto.dto.AdminUserDTO;
import org.ros.chatto.model.ChatUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<ChatUser, Long> {
	@Query("select cu from ChatUser cu join fetch cu.userRoles where cu.userName = ?1")
	public Optional<ChatUser> findByUserName(String userName);

	@Query("select cu.userName from ChatUser cu where cu.userName != ?1")
	public List<String> findAllOtherUserNames(String userName);

	@Query("select count(u) from ChatUser u")
	public Long totalUsers();

	@Query("select new org.ros.chatto.dto.AdminUserDTO(u.userID, u.userName, ur.role.name, u.joinDate )"
			+ " from ChatUser u join u.userRoles ur ")
	public List<AdminUserDTO> getOtherUsers(String principal);
}
