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

import com.sikey.wa04.business.infrastructure.dto.model.DeviceModel;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
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.Optional;

@Repository
public interface DeviceRepository extends JpaRepository<DeviceModel, Long> {
    Optional<DeviceModel> findByTicket(@Size(max = 30) @NotNull String ticket);

    Optional<DeviceModel> findByHc(@Size(max = 30) @NotNull String hc);

    @Transactional
    @Modifying
    @Query("UPDATE DeviceModel d SET d.version = :version,d.model = :model WHERE d.id = :id")
    int updateVersionAndModelById(@Param("id") Long id, @Param("version") String version, @Param("model") String model);
}
