package com.alterdekim.hearthhack.repository;

import com.alterdekim.hearthhack.dto.UserCardDTO;
import com.alterdekim.hearthhack.entity.UserCard;
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 UserCardRepository extends JpaRepository<UserCard, Long> {

    @Query(nativeQuery = true)
    List<UserCardDTO> getCollectionOfUser(@Param("userId") Long userId);

    @Modifying
    @Transactional
    @Query(value = "UPDATE UserCard u SET u.hasSeen = :hasSeen WHERE u.userId = :userId AND u.assetId = :assetId AND u.isPremium = :isPremium")
    void updateSeenByUserIdAndCard(@Param("userId") Long userId, @Param("assetId") Long assetId, @Param("isPremium") Boolean isPremium, @Param("hasSeen") Boolean hasSeen);

    @Modifying
    @Transactional
    @Query(value = "DELETE FROM UserCard u WHERE u.id IN (SELECT MAX(b.id) FROM UserCard b WHERE b.userId = :userId AND b.assetId = :assetId AND b.isPremium = :isPremium)")
    void remove(@Param("userId") Long userId, @Param("assetId") Long assetId, @Param("isPremium") Boolean isPremium);
}
