package com.ttstd.signaling.store;

import com.ttstd.signaling.model.PairingCode;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/** 内存实现，用于测试与本地演示（storage.mode=memory）。 */
@Component
@Profile("memory")
public class InMemoryPairingStore implements PairingStore {

    private final ConcurrentMap<String, PairingCode> byHash = new ConcurrentHashMap<>();

    @Override
    public synchronized PairingCode save(PairingCode code) {
        byHash.put(code.getCodeHash(), code);
        return code;
    }

    @Override
    public synchronized Optional<PairingCode> findByCodeHash(String codeHash) {
        return Optional.ofNullable(byHash.get(codeHash));
    }

    @Override
    public synchronized void delete(String codeHash) {
        byHash.remove(codeHash);
    }
}
