package com.ttstd.signaling.store;

import com.ttstd.signaling.model.DeviceBlacklistEntry;
import com.ttstd.signaling.repository.DeviceBlacklistRepository;
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 JpaBlacklistStore implements BlacklistStore {

    private final DeviceBlacklistRepository repository;

    public JpaBlacklistStore(DeviceBlacklistRepository repository) {
        this.repository = repository;
    }

    @Override
    public DeviceBlacklistEntry save(DeviceBlacklistEntry entry) {
        return repository.save(entry);
    }

    @Override
    public Optional<DeviceBlacklistEntry> findByDeviceUidAndBlockedUserId(
            String deviceUid, String blockedUserId) {
        return repository.findByDeviceUidAndBlockedUserId(deviceUid, blockedUserId);
    }

    @Override
    public boolean existsByDeviceUidAndBlockedUserId(String deviceUid, String blockedUserId) {
        return repository.existsByDeviceUidAndBlockedUserId(deviceUid, blockedUserId);
    }

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

    @Override
    public void delete(String deviceUid, String blockedUserId) {
        repository.deleteById(new DeviceBlacklistEntry.PK(deviceUid, blockedUserId));
    }
}
