package com.alterdekim.game.repository;

import com.alterdekim.game.component.game.PlayerProperties;
import com.alterdekim.game.entity.UserProperty;
import jakarta.transaction.Transactional;
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 java.util.List;
import java.util.Optional;

@Repository
public interface UserPropertyRepository extends JpaRepository<UserProperty, Long> {
    Optional<UserProperty> findById(Long id);

    @Query(value = "SELECT u FROM UserProperty u WHERE u.user.id = :uid AND u.type = :type")
    Optional<UserProperty> findByUserIdAndTag(@Param("uid") Integer userId, @Param("type") PlayerProperties property);

    List<UserProperty> findAll();

    @Transactional
    @Modifying
    @Query(value = "DELETE FROM UserProperty u WHERE u.user.id = :uid AND u.type = :type")
    void deleteByUserIdAndType(@Param("uid") Integer userId, @Param("type") PlayerProperties properties);
}
