package org.operaton.fitpub.repository;

import org.operaton.fitpub.model.entity.User;
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 org.springframework.stereotype.Repository;

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

/**
 * Repository for User entity.
 * Provides methods for user authentication and discovery.
 */
@Repository
public interface UserRepository extends JpaRepository<User, UUID> {

    /**
     * Finds a user by username.
     * Used for login and WebFinger discovery.
     *
     * @param username the username
     * @return optional user
     */
    Optional<User> findByUsername(String username);

    /**
     * Finds a user by email.
     * Used for login and duplicate email checking.
     *
     * @param email the email address
     * @return optional user
     */
    Optional<User> findByEmail(String email);

    /**
     * Checks if a username already exists.
     * Used during registration.
     *
     * @param username the username to check
     * @return true if username exists
     */
    boolean existsByUsername(String username);

    /**
     * Checks if an email already exists.
     * Used during registration.
     *
     * @param email the email to check
     * @return true if email exists
     */
    boolean existsByEmail(String email);

    /**
     * Finds all enabled users.
     * Used for administrative purposes.
     *
     * @return list of enabled users
     */
    Optional<User> findByUsernameAndEnabledTrue(String username);

    /**
     * Searches for users by username or display name (case-insensitive).
     * Used for user discovery.
     *
     * @param query the search query
     * @param pageable pagination parameters
     * @return page of matching users
     */
    @Query("SELECT u FROM User u WHERE u.enabled = true AND " +
           "(LOWER(u.username) LIKE LOWER(CONCAT('%', :query, '%')) OR " +
           "LOWER(u.displayName) LIKE LOWER(CONCAT('%', :query, '%')))")
    Page<User> searchUsers(@Param("query") String query, Pageable pageable);

    /**
     * Finds all enabled users with pagination.
     * Used for browsing all users.
     *
     * @param pageable pagination parameters
     * @return page of enabled users
     */
    @Query("SELECT u FROM User u WHERE u.enabled = true ORDER BY u.createdAt DESC")
    Page<User> findAllEnabledUsers(Pageable pageable);
}
