package com.izouma.nineth.repo;

import com.izouma.nineth.domain.AuctionActivity;
import com.izouma.nineth.enums.AuctionStatus;
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 AuctionActivityRepo extends JpaRepository<AuctionActivity, Long>, JpaSpecificationExecutor<AuctionActivity> {
    @Query("update AuctionActivity t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

    @Query("select status from AuctionActivity where id = ?1")
    String getStatus(Long id);

    @Modifying
    @Transactional
    @Query("update AuctionActivity set status = ?2 where id = ?1")
    void updateStatus(Long id, AuctionStatus status);

    @Query("update AuctionActivity t set t.likes = t.likes + ?2 where t.id = ?1")
    @Modifying
    @Transactional
    void addLike(Long id, int num);

    @Transactional
    @Modifying
    @Query("update AuctionActivity c set c.status = ?2 where c.id = ?1")
    void scheduleOffShelf(Long id, AuctionStatus status);

    List<AuctionActivity> findAllByStatus(AuctionStatus auctionStatus);

    List<AuctionActivity> findByAssetId(Long assetId);

    List<AuctionActivity> findByStartTimeBeforeAndStatusIn(LocalDateTime startTime, Collection<AuctionStatus> status);

    List<AuctionActivity> findByStartTimeBeforeAndStatus(LocalDateTime startTime, AuctionStatus status);

    @Modifying
    @Transactional
    @Query("update AuctionActivity set onShelf = ?2 where assetId = ?1 and onShelf <> ?2")
    void updateOnShelf(Long assetId, boolean onShelf);
}