package de.votian.services.repository;

import de.votian.services.entity.GenericDataContainer;
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 java.util.List;
import java.util.Optional;

public interface GenericDataContainerRepository extends JpaRepository<GenericDataContainer, Long> {

    @Query("SELECT g FROM GenericDataContainer g WHERE g.objectType = :type AND g.objectId = :objId AND g.fieldName = :field")
    Optional<GenericDataContainer> findByTypeAndIdAndField(
            @Param("type") String type, @Param("objId") Long objId, @Param("field") String field);

    @Query("SELECT g FROM GenericDataContainer g WHERE g.objectType = :type AND g.objectId = :objId")
    List<GenericDataContainer> findByTypeAndId(@Param("type") String type, @Param("objId") Long objId);

    @Modifying
    @Query("UPDATE GenericDataContainer g SET g.content = :content, g.context = :context " +
           "WHERE g.objectType = :type AND g.objectId = :objId AND g.fieldName = :field")
    void updateContent(@Param("type") String type, @Param("objId") Long objId,
                       @Param("field") String field, @Param("content") String content, @Param("context") String context);
}
