package com.sikey.wa04.business.infrastructure.repository;

import com.sikey.wa04.business.infrastructure.dto.model.DeviceConfigModel;
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.time.Instant;

@Repository
public interface DeviceConfigRepository extends JpaRepository<DeviceConfigModel, Long> {
    DeviceConfigModel findByDeviceIdAndKey(Long deviceId, String key);

    @Transactional
    @Modifying
    @Query("UPDATE DeviceConfigModel d SET d.value = :value, d.beforeValue = :beforeValue, d.updatedAt = :updatedAt WHERE d.deviceId = :deviceId AND d.key = :key")
    int updateByDeviceIdAndKey(
            @Param("deviceId") Long deviceId,
            @Param("key") String key,
            @Param("value") String value,
            @Param("beforeValue") String beforeValue,
            @Param("updatedAt") Instant updatedAt
    );
}
