package com.alterdekim.hearthhack.repository;

import com.alterdekim.hearthhack.dto.BoosterDTO;
import com.alterdekim.hearthhack.entity.Booster;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface BoosterRepository extends JpaRepository<Booster, Long> {
    @Query(nativeQuery = true)
    List<BoosterDTO> findBoostersByUserId(@Param("userId") Long userId);

    @Transactional
    @Modifying
    @Query(value = "DELETE FROM Booster a WHERE a.id IN (SELECT c.lid FROM (SELECT MAX(b.id) AS lid FROM Booster b WHERE b.userId = :userId AND b.type = :type) c)")
    void deleteOneByType(@Param("userId") Long userId, @Param("type") Integer type);
}
