package com.alterdekim.game.repository;

import com.alterdekim.game.entity.HouseInventory;
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;

@Repository
public interface HouseInventoryRepository extends JpaRepository<HouseInventory, Long> {

    @Query(value = "SELECT h FROM HouseInventory h WHERE h.user.id = :uid")
    List<HouseInventory> findByUserId(@Param("uid") int userId);

    @Transactional
    @Modifying
    @Query(value = "UPDATE HouseInventory h SET h.homeId = :houseId, h.x = :x, h.y = :y, h.frame = :frame WHERE h.id = :uid")
    void updateInfoById(@Param("uid") Long id, @Param("houseId") Long houseId, @Param("x") Double x, @Param("y") Double y, @Param("frame") Integer frame);

    @Query(value = "SELECT h FROM HouseInventory h WHERE h.user.id = :uid AND h.homeId = :houseId")
    List<HouseInventory> findByUserIdAndHouseId(@Param("uid") Integer userId, @Param("houseId") Long houseId);

    @Transactional
    @Modifying
    @Query(value = "UPDATE HouseInventory h SET h.homeId = -1 WHERE h.id = :uid")
    void resetHomeId(@Param("uid") Long id);
}
