package com.honey.honey.repository;

import com.honey.honey.model.Promotion;
import com.honey.honey.model.Promotion.PromotionStatus;
import com.honey.honey.model.Promotion.PromotionType;
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.time.Instant;
import java.util.List;

@Repository
public interface PromotionRepository extends JpaRepository<Promotion, Integer> {

    /**
     * Find all promotions of given type that are ACTIVE and current time is within [start_time, end_time].
     */
    @Query("SELECT p FROM Promotion p WHERE p.type = :type AND p.status = 'ACTIVE' " +
           "AND p.startTime < :now AND p.endTime > :now")
    List<Promotion> findActiveByTypeAndTimeRange(
            @Param("type") PromotionType type,
            @Param("now") Instant now);

    List<Promotion> findAllByOrderByStartTimeDesc();

    /** For app: list promotions with status in ACTIVE, PLANNED, FINISHED (exclude INACTIVE). */
    List<Promotion> findByStatusInOrderByStartTimeDesc(List<PromotionStatus> statuses);
}
