package com.alterdekim.hearthhack.repository;

import com.alterdekim.hearthhack.entity.Deck;
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 DeckRepository extends JpaRepository<Deck, Long> {

    @Query(value = "SELECT new Deck(d.id, d.userId, d.name, d.cardBack, d.hero, d.deckType, d.heroPremium, d.cardBackOverride, d.heroOverride, d.lastModified, d.sortOrder, d.sourceType) FROM Deck d WHERE d.userId = :userId")
    List<Deck> findDecksByUserId(@Param("userId") Long userId);

    @Query(value = "UPDATE Deck d SET d.name = :deckName WHERE d.userId = :userId AND d.id = :deckId")
    @Transactional
    @Modifying
    void updateDeckName(@Param("userId") Long userId, @Param("deckId") Long deckId, @Param("deckName") String name);


    @Transactional
    @Modifying
    @Query(value = "UPDATE Deck d SET d.cardBack = :cardBack WHERE d.userId = :userId AND d.id = :deckId")
    void updateDeckCardBack(@Param("userId") Long userId, @Param("deckId") Long deckId, @Param("cardBack") Integer cardBack);

    @Transactional
    @Modifying
    @Query(value = "UPDATE Deck d SET d.heroPremium = :isPremium, d.hero = :heroId WHERE d.userId = :userId AND d.id = :deckId")
    void updateDeckHero(@Param("userId") Long userId, @Param("deckId") Long deckId, @Param("heroId") Integer heroId, @Param("isPremium") Boolean isPremium);

    @Transactional
    @Modifying
    @Query(value = "DELETE FROM Deck d WHERE d.id = :deckId AND d.userId = :userId")
    void deleteDeck(@Param("userId") Long userId, @Param("deckId") Long deckId);
}
