package com.izouma.nineth.repo;

import com.izouma.nineth.domain.AuctionOrder;
import com.izouma.nineth.enums.AuctionPaymentType;
import com.izouma.nineth.enums.AuctionOrderStatus;
import com.izouma.nineth.enums.AuctionStatus;
import com.izouma.nineth.service.AuctionOrderService;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

public interface AuctionOrderRepo extends JpaRepository<AuctionOrder, Long>, JpaSpecificationExecutor<AuctionOrder> {
    @Query("update AuctionOrder t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

    AuctionOrder findByUserIdAndAuctionIdAndPaymentTypeAndStatusIn(Long userId, Long auctionId, AuctionPaymentType paymentType, Collection<AuctionOrderStatus> status);

    List<AuctionOrder> findAllByAuctionIdAndPaymentTypeAndStatus(Long auctionId, AuctionPaymentType paymentType, AuctionOrderStatus status);

    List<AuctionOrder> findByStatusAndCreatedAtBeforeAndDelFalse(AuctionOrderStatus status, LocalDateTime createdAt);

    AuctionOrder findFirstByAuctionRecordIdOrderByCreatedAt(Long recordId);

    AuctionOrder findFirstByAuctionRecordIdOrderByIdDesc(Long recordId);

    AuctionOrder findFirstByAuctionIdAndPaymentTypeAndStatusNotInAndUserIdOrderByIdDesc(Long auctionId, AuctionPaymentType paymentType, Collection<AuctionOrderStatus> status, Long userId);
}