package com.ttstd.signaling.store;

import com.ttstd.signaling.model.DeviceBinding;
import com.ttstd.signaling.repository.DeviceBindingRepository;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/** JPA 实现：绑定关系持久化到 MySQL。仅在 storage.mode=jpa（默认）时生效。 */
@Component
@Profile("!memory")
public class JpaBindingStore implements BindingStore {

    private final DeviceBindingRepository repository;

    public JpaBindingStore(DeviceBindingRepository repository) {
        this.repository = repository;
    }

    @Override
    public DeviceBinding save(DeviceBinding binding) {
        return repository.save(binding);
    }

    @Override
    public Optional<DeviceBinding> findByDeviceUidAndUserId(String deviceUid, String userId) {
        return repository.findByDeviceUidAndUserId(deviceUid, userId);
    }

    @Override
    public List<DeviceBinding> listByDeviceUid(String deviceUid) {
        return repository.findByDeviceUid(deviceUid);
    }

    @Override
    public List<DeviceBinding> listByUserId(String userId) {
        return repository.findByUserId(userId);
    }

    @Override
    public void delete(String bindingId) {
        repository.deleteById(bindingId);
    }
}
