package de.cannamanage.service.repository;

import de.cannamanage.domain.entity.Member;
import de.cannamanage.domain.enums.MemberStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

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

@Repository
public interface MemberRepository extends JpaRepository<Member, UUID> {

    long countByTenantId(UUID tenantId);

    long countByTenantIdAndStatus(UUID tenantId, MemberStatus status);

    /**
     * Find all members for a tenant, optionally filtered by status.
     */
    List<Member> findByTenantIdAndStatus(UUID tenantId, MemberStatus status);

    /**
     * Find all members for a tenant (all statuses).
     */
    List<Member> findByTenantId(UUID tenantId);

    /**
     * Find all under-21 members for a tenant.
     */
    List<Member> findByTenantIdAndUnder21True(UUID tenantId);

    java.util.Optional<Member> findByUserId(UUID userId);

    /**
     * Alias for findByTenantId — in CannaManage, clubId == tenantId.
     */
    default List<Member> findByClubId(UUID clubId) {
        return findByTenantId(clubId);
    }

    default List<Member> findAllByClubId(UUID clubId) {
        return findByTenantId(clubId);
    }

    /**
     * Get all active member user IDs (for broadcast notifications).
     * Uses the Hibernate tenant filter, so no explicit tenantId parameter needed.
     */
    @Query("SELECT m.userId FROM Member m WHERE m.status = 'ACTIVE'")
    List<UUID> findAllActiveUserIds();
}
