package br.gov.sigefp.admin.repository;

import br.gov.sigefp.admin.domain.UserAccount;
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.stereotype.Repository;

import java.util.Optional;
import java.util.UUID;

@Repository
public interface UserAccountRepository extends JpaRepository<UserAccount, UUID> {
    
    /**
     * Busca todos os utilizadores com seus perfis carregados eagerly.
     * Usa JOIN FETCH para evitar lazy loading exceptions.
     */
    @Query("SELECT DISTINCT u FROM UserAccount u LEFT JOIN FETCH u.userRoles ur LEFT JOIN FETCH ur.role")
    Page<UserAccount> findAllWithRoles(Pageable pageable);
    
    Optional<UserAccount> findByUsername(String username);
    
    Optional<UserAccount> findByEmail(String email);
    
    boolean existsByUsername(String username);
    
    boolean existsByEmail(String email);
}

