package com.kt.event.eventservice.domain.repository;

import com.kt.event.eventservice.domain.entity.Event;
import com.kt.event.eventservice.domain.enums.EventStatus;
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.Optional;
import java.util.UUID;

/**
 * 이벤트 Repository
 *
 * @author Event Service Team
 * @version 1.0.0
 * @since 2025-10-23
 */
@Repository
public interface EventRepository extends JpaRepository<Event, UUID> {

    /**
     * 사용자 ID와 이벤트 ID로 조회
     */
    @Query("SELECT DISTINCT e FROM Event e " +
           "LEFT JOIN FETCH e.channels " +
           "WHERE e.eventId = :eventId AND e.userId = :userId")
    Optional<Event> findByEventIdAndUserId(
        @Param("eventId") UUID eventId,
        @Param("userId") UUID userId
    );

    /**
     * 사용자별 이벤트 목록 조회 (페이징, 상태 필터)
     */
    @Query("SELECT e FROM Event e " +
           "WHERE e.userId = :userId " +
           "AND (:status IS NULL OR e.status = :status) " +
           "AND (:search IS NULL OR e.eventName LIKE %:search%) " +
           "AND (:objective IS NULL OR e.objective = :objective)")
    Page<Event> findEventsByUser(
        @Param("userId") UUID userId,
        @Param("status") EventStatus status,
        @Param("search") String search,
        @Param("objective") String objective,
        Pageable pageable
    );

    /**
     * 사용자별 이벤트 개수 조회 (상태별)
     */
    long countByUserIdAndStatus(UUID userId, EventStatus status);
}
